POJ 1573 Robot Motion(小模拟)

题目链接

赤裸裸的模拟,判断终止语句放错,2Y。

 1 #include <stdio.h>
 2 #include <string.h>
 3 int main()
 4 {
 5     int n,m,s,i,num,x,y;
 6     char p[21][21];
 7     int o[21][21];
 8     while(scanf("%d%d%d%*c",&n,&m,&s)!=EOF)
 9     {
10         if(!n&&!m&&!s) break;
11         num = 1;
12         memset(o,0,sizeof(o));
13         for(i = 0;i <= n-1;i ++)
14         gets(p[i]);
15         x = 0;
16         y = s-1;
17         for(;;)
18         {
19             if(x >= n||y >=m||x < 0||y < 0)
20             {
21                 printf("%d step(s) to exit\n",num-1);
22                 break;
23             }
24             if(o[x][y])
25             {
26                 printf("%d step(s) before a loop of %d step(s)\n",o[x][y]-1,num-o[x][y]);
27                 break;
28             }
29             o[x][y] = num;
30             if(p[x][y] == 'E')
31             {
32                 y = y+1;
33             }
34             else if(p[x][y] == 'W')
35             {
36                 y = y-1;
37             }
38             else if(p[x][y] == 'S')
39             {
40                 x = x+1;
41             }
42             else if(p[x][y] == 'N')
43             {
44                 x = x-1;
45             }
46             num ++;
47         }
48     }
49     return 0;
50 }
posted @ 2012-07-15 19:58  Naix_x  阅读(165)  评论(0)    收藏  举报