poj1573 Robot Motion 模拟水题

 1 ///2014.3.9
 2 ///poj1573
 3 
 4 /**
 5  *模拟水题,比poj2632还水
 6  */
 7 
 8 #include <iostream>
 9 #include <cstdio>
10 #include <cstring>
11 using namespace std;
12 
13 int main()
14 {
15 //    freopen("in","r",stdin);
16 //    freopen("out","w",stdout);
17 
18     int L,W;
19     int first;
20     while( cin>>L>>W>>first && (W||L||first) ){
21         char direction[L+1][W+1];
22         int step[L+1][W+1];
23         for(int i=1 ; i<=L ; i++){
24             for(int j=1 ; j<=W ; j++){
25                 cin>>direction[i][j];
26             }
27         }
28         memset( step,0,sizeof(step) );
29 
30         bool stop = false;
31         bool loop = false;
32         int now_x = 1,now_y = first;
33         int next_x,next_y;
34         int num = 1;
35         step[now_x][now_y] = num;
36         while( !stop ){
37             switch( direction[now_x][now_y] ){
38             case 'E':
39                 {
40                     next_x = now_x;
41                     next_y = now_y+1;
42                 } break;
43             case 'W':
44                 {
45                     next_x = now_x;
46                     next_y = now_y-1;
47                 } break;
48             case 'S':
49                 {
50                     next_x = now_x+1;
51                     next_y = now_y;
52                 } break;
53             case 'N':
54                 {
55                     next_x = now_x-1;
56                     next_y = now_y;
57                 } break;
58             }
59             if( next_x<1 || next_x>L || next_y<1 || next_y>W )
60                 break;
61             else if( step[next_x][next_y] ){
62                 loop = true;
63                 num++;
64                 break;
65             }
66             else{
67                 step[next_x][next_y] = ++num;
68                 now_x = next_x;
69                 now_y = next_y;
70             }
71         }
72         if( loop )
73             printf("%d step(s) before a loop of %d step(s)\n",step[next_x][next_y]-1,step[now_x][now_y]-step[next_x][next_y]+1);
74         else
75             printf("%d step(s) to exit\n",step[now_x][now_y]);
76     }
77     return 0;
78 }

 

posted @ 2014-03-09 19:51  basement_boy  阅读(163)  评论(0编辑  收藏  举报