Posted on 2008-05-26 15:08
superman 阅读(159)
评论(0) 编辑 收藏 引用 所属分类:
ZOJ
1 /* Accepted 1708 C++ 00:00.00 836K */
2 #include <iostream>
3
4 using namespace std;
5
6 int main()
7 {
8 int n, m, start;
9 while(cin >> n >> m >> start)
10 {
11 if(n == 0 && m == 0 && start == 0)
12 break;
13
14 char map[10][10];
15 for(int i = 0; i < n; i++)
16 for(int j = 0; j < m; j++)
17 cin >> map[i][j];
18
19 int sx, sy;
20 for(int i = 0; i < n; i++)
21 for(int j = 0; j < m; j++)
22 {
23 start--;
24 if(start == 0)
25 {
26 sx = i, sy = j; break;
27 }
28 }
29
30 int cnt[10][10] = { 0 };
31 cnt[sx][sy] = 1;
32
33 while(true)
34 {
35 int tx = sx;
36 int ty = sy;
37
38 switch(map[sx][sy])
39 {
40 case 'N' : tx--; break;
41 case 'S' : tx++; break;
42 case 'W' : ty--; break;
43 case 'E' : ty++; break;
44 }
45 if(tx < 0 || tx == n || ty < 0 || ty == m)
46 {
47 cout << cnt[sx][sy] << " step(s) to exit" << endl;
48 break;
49 }
50 if(cnt[tx][ty])
51 {
52 cout << cnt[tx][ty] - 1 << " step(s) before a loop of "
53 << cnt[sx][sy] - cnt[tx][ty] + 1 << " step(s)" << endl;
54 break;
55 }
56 cnt[tx][ty] = cnt[sx][sy] + 1;
57 sx = tx, sy = ty;
58 }
59 }
60
61 return 0;
62 }
63