poj 1573Robot Motion

http://poj.org/problem?id=1573

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define maxn 500
 5 using namespace std;
 6 char g[maxn][maxn];
 7 int r,l,st;
 8 int vis[maxn][maxn];
 9 int main()
10 {
11     while(scanf("%d%d%d",&r,&l,&st)!=EOF)
12     {
13         if(r==0&&l==0&&st==0) break;
14         for(int i=0; i<r; i++)
15         {
16             scanf("%s",g[i]);
17         }
18         int si=0,sj=st-1;
19         int step=1,step1;
20         bool flag=false;
21         memset(vis,0,sizeof(vis));
22         while(1)
23         {
24             if(vis[si][sj])
25             {
26                 flag=true;
27                 step1=step-vis[si][sj];
28                 break;
29             }
30             if((g[si][sj]=='W'&&sj==0)||(g[si][sj]=='E'&&sj==l-1)||(g[si][sj]=='S'&&si==r-1)||(g[si][sj]=='N'&&si==0)) break;
31             vis[si][sj]=step;
32             if(g[si][sj]=='W')
33             {
34                 sj--;
35             }
36             else if(g[si][sj]=='E')
37             {
38                 sj++;
39             }
40             else if(g[si][sj]=='S')
41             {
42                 si++;
43             }
44             else if(g[si][sj]=='N')
45             {
46                 si--;
47             }
48             step++;
49         }
50         if(flag)
51         {
52             //printf("%d %d
",si,sj);
53             printf("%d step(s) before a loop of %d step(s)
",vis[si][sj]-1,step1);
54         }
55         else
56             printf("%d step(s) to exit
",step);
57     }
58     return 0;
59 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/3524795.html