POJ1573Robot Motion (+ simple simple simulation DFS)

Title here

The meaning of problems

According to ask you figure given tips go, how many steps can come out? ? ?

Based on this fact, as long as the tips go on the line. Analog every step on OK, because the next step and the step of operating the same, so simple dfs. If a loop condition occurs, the first few steps, and finally the total number of steps to lose it as long as each of the memory to the point

  1  / * *********************************************** *************************
   2      > the Name File: poj1573.cpp
   . 3      > the Author: YeGuoSheng
   . 4      > the Description:  
   . 5      to a start position and a mark each toward the maze, asked if he could follow every step of
   6      out of the labyrinth tips
   7      > the Created Time: 2019 Nian 07 Tuesday, 23 October 17:27:34
   8  ************* ************************************************** ******** * / 
  . 9  
10 #include <the iostream>
 . 11 #include <stdio.h>
 12 is #include <CString>
 13 is #include <the cmath>
 14 #include <Vector>
 15#include <Stack>
 16 #include <Map>
 . 17 #include < SET >
 18 is #include <List>
 . 19 #include <Queue>
 20 is #include < String >
 21 is #include <algorithm>
 22 is #include <The iomanip>
 23 is  the using  namespace STD;
 24  const  int MAXN = 20 is ;
 25  char G [MAXN] [MAXN];
 26 is  int VIS [MAXN] [MAXN];
 27  int gcount [MAXN] [MAXN]; // tag the access point to the first few step access to the 
28  intRow, COL, Start;
 29  int the Count = 0 ;
 30  int T = 0 ;
 31 is  BOOL In Flag = to false ; // tag is out of the dead or unreal 
32  int P; // a number of steps reaches a certain position on the record, preventing is covered with the second number of steps to reach 
33 is  
34 is  void DFS2 ( int X, int Y, int the Count)
 35  {
 36      IF (X == . 1 && == Y start) // return to the starting position of the end DFS 
37 [      {
 38 is          return ;
 39     }
 40  }
 41 is  
42 is  void DFS ( int X, int Y, int the Count)
 43 is  {
 44 is      T = the Count; // record the total number of steps 
45      P = gcount [X] [Y]; // advance stored in a first the number of steps to reach the point 
46 is      gcount [X] [Y] = T; // updated to the current access point of the total number of steps 
47      IF (X == 0 || Y == 0 || X == Row + . 1 || COL + == Y . 1 ) // out of the case to 
48      {
 49          in Flag = to true ;
50          return ;
 51 is      }
 52 is      IF (VIS [X] [Y] == 0 ) // if the current node is not accessed 
53 is      {
 54 is          VIS [X] [Y] = 1 ; // marked as 1, and then performs the following the DFS 
55      }
 56 is      the else  // . 1 have visited the current position, i.e., the next constituting the infinite loop 
57 is      {
 58          in Flag = to false ;
 59          return ;
 60      }
 61 is      IF (G [X] [Y] == ' W is ' ) // a position of the lower DFS 
62 is     {
 63         dfs(x,y-1,Count+1);
 64         return ;
 65     }
 66     else if(g[x][y]=='E')
 67     {
 68         dfs(x,y+1,Count+1);
 69         return ;
 70     }
 71     else if (g[x][y] =='S')
 72     {
 73         dfs(x+1,y,Count+1);
 74         return;
 75     }
 76     else//N
 77     {
 78         dfs(x-1,y,Count+1);
 79         return ;
 80     }
 81 }
 82 
 83 int main()
 84 {
 85     while(scanf("%d%d%d",&row,&col,&start) && row != 0 && col != 0 && start != 0)
 86     {
 87         Count=0;
 88          P = 0 ;
 89          T = 0 ;
 90          Memset (G, ' . ' , The sizeof (G)); // '.' Border filling 
91 is          Memset (VIS, 0 , the sizeof (VIS));
 92          for ( int I = . 1 ; I <= Row; I ++) // whole Grid boundary added 
93          {
 94              for ( int J = . 1 ; J <= COL; J ++ )
 95              {
 96                 cin>>g[i][j];
 97             }
 98         } 
 99         dfs(1,start,0);
100         if(flag)
101         {
102             // for(int i = 1;i <= row;i++)
103             // {
104             //     for(int j= 1;j <= col;j++)
105             //     {
106             //         cout<<gCount[i][j]<<" ";
107             //     }
108             //     cout<<endl;
109             // }
110             cout<<t<<" step(s) to exit"<<endl;
111         }
112         else
113         {
114             cout<<p<<" step(s) before a loop of "<<t-p<<" step(s)"<<endl;
115         }
116     }
117     return 0;
118 }
View Code

 

Guess you like

Origin www.cnblogs.com/ygsworld/p/11233705.html