问题:
http://poj.org/problem?id=1573思路:
简单题,纯模拟...
代码:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define MAX_LEN 12
5 #define is_valid(x, y) (x>=0 && x<R && y>=0 && y<C)
6 char map[MAX_LEN][MAX_LEN];
7 int steps[MAX_LEN][MAX_LEN];
8 int R, C, entry;
9
10 void
11 solve()
12 {
13 char ch;
14 int cx, cy, px, py;
15 cx = px = 0;
16 cy = py = entry-1;
17 while(is_valid(cx, cy) && !steps[cx][cy]) {
18 steps[cx][cy] = steps[px][py] + 1;
19 ch = map[cx][cy];
20 px = cx;
21 py = cy;
22 switch(ch) {
23 case 'N':
24 cx = px-1;
25 cy = py;
26 break;
27 case 'S':
28 cx = px+1;
29 cy = py;
30 break;
31 case 'W':
32 cx = px;
33 cy = py-1;
34 break;
35 case 'E':
36 cx = px;
37 cy = py+1;
38 break;
39 }
40 }
41 if(!is_valid(cx, cy))
42 printf("%d step(s) to exit\n", steps[px][py]);
43 else if(steps[cx][cy])
44 printf("%d step(s) before a loop of %d step(s)\n", steps[cx][cy]-1, steps[px][py]-steps[cx][cy]+1);
45 }
46
47 int
48 main(int argc, char **argv)
49 {
50 int i;
51 while(scanf("%d %d %d", &R, &C, &entry)!=EOF && R) {
52 for(i=0; i<R; i++)
53 scanf("%s", map[i]);
54 memset(steps, 0, sizeof(steps));
55 solve();
56 }
57 }