HDOJ1035

  1. /*
  2. *这是杭电OJ上的一道简单题
  3. *C++实现
  4. *第一次尝试用面向对象的思想去做题,感觉很不错
  5. */
  6. #include <iostream>
  7. using namespace std;
  8. class Node
  9. {
  10. public:
  11.     Node()//construction
  12.     {
  13.         time=0;
  14.         flag=false;
  15.     }
  16.     void SetAction(char a)
  17.     {
  18.         action=a;
  19.     }
  20.     char GetAction()
  21.     {
  22.         return action;
  23.     }
  24.     void SetTime(int t)
  25.     {
  26.         time=t;
  27.     }
  28.     int GetTime()
  29.     {
  30.         return time;
  31.     }
  32.     void SetFlag(bool f)
  33.     {
  34.         flag=f;
  35.     }
  36.     int GetFlag()
  37.     {
  38.         return flag;
  39.     }   
  40. private:
  41.     char action;
  42.     int time;
  43.     bool flag;
  44. };
  45. int main()
  46. {
  47.     int i,j;
  48.     int row,col,sCol,curRow,curCol,nextRow,nextCol;
  49.     int exitTime,loopStartT,loopTime;
  50.     char action;    
  51.     int flag;
  52.     enum{exit=1,notExit=2};
  53.     while(cin>>row>>col)
  54.     {
  55.         if(row==0 && col==0)
  56.             break;
  57.         cin>>sCol;
  58.         Node nodes[10][10];
  59.         for(i=0;i<row;i++)
  60.         {
  61.             for(j=0;j<col;j++)
  62.             {
  63.                 cin>>action;
  64.                 nodes[i][j].SetAction(action);
  65.             }
  66.         }
  67.         exitTime=-1;
  68.         curRow=0;
  69.         curCol=sCol-1;
  70.         nodes[curRow][curCol].SetTime(0);
  71.         nodes[curRow][curCol].SetFlag(true);    
  72.         flag=0;
  73.         while(1)
  74.         {                   
  75.             switch(nodes[curRow][curCol].GetAction())
  76.             {
  77.             case 'N':
  78.                 nextRow=curRow-1;
  79.                 nextCol=curCol;
  80.                 break;
  81.             case 'W':
  82.                 nextRow=curRow;
  83.                 nextCol=curCol-1;
  84.                 break;
  85.             case 'S':
  86.                 nextRow=curRow+1;
  87.                 nextCol=curCol;
  88.                 break;
  89.             case 'E':
  90.                 nextRow=curRow;
  91.                 nextCol=curCol+1;
  92.                 break;
  93.             }
  94.             
  95.             if(nextRow<0 || nextRow>=row || nextCol<0 || nextCol>=col)//out of grid
  96.             {
  97.                 exitTime=nodes[curRow][curCol].GetTime()+1;
  98.                 flag=exit;
  99.                 break;
  100.             }
  101.             if(nodes[nextRow][nextCol].GetFlag())//loop
  102.             {
  103.                 loopStartT=nodes[nextRow][nextCol].GetTime();
  104.                 loopTime=nodes[curRow][curCol].GetTime()-loopStartT+1;
  105.                 flag=notExit;
  106.                 break;
  107.             }
  108.             nodes[nextRow][nextCol].SetTime(nodes[curRow][curCol].GetTime()+1);
  109.             nodes[curRow][curCol].SetFlag(true);
  110.             curRow=nextRow;
  111.             curCol=nextCol;
  112.         }
  113.         if(flag==exit)
  114.             cout<<exitTime<<" step(s) to exit"<<endl;
  115.         if(flag==notExit)
  116.             cout<<loopStartT<<" step(s) before a loop of "<<loopTime<<" step(s)"<<endl;
  117.     }
  118.     return 0;
  119. }

你可能感兴趣的:(c,action)