POJ 1573 Robot Motion

模拟水题,Code:

1 #include<iostream>
2  using namespace std;
3
4 struct Dir
5 {
6 int y,x;
7 };
8 int main()
9 {
10 const Dir dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
11 int M,N,E, vx, vy, step;
12 char ch;
13 char grid[10][10];
14 while (cin >> M >> N >> E && (M*N*E))
15 {
16 int visit[10][10] = {0};
17
18 for (int i = 0; i < M; i++)
19 {
20 for (int j = 0; j < N; j++)
21 {
22 cin >> ch;
23 switch (ch)
24 {
25 case 'E':
26 grid[i][j] = 0;
27 break;
28 case 'S':
29 grid[i][j] = 1;
30 break;
31 case 'W':
32 grid[i][j] = 2;
33 break;
34 default:
35 grid[i][j] = 3;
36 }
37 }
38 }
39 vx = E - 1, vy = 0;
40 visit[vy][vx] = 1;
41 step = 1;
42 while (true)
43 {
44 int temp = grid[vy][vx];
45 vy += dir[temp].y;
46 vx += dir[temp].x;
47 if (vy < 0 || vy >= M || vx < 0 || vx >= N)
48 {
49 cout << step << " step(s) to exit" << endl;
50 break;
51 }
52 if (visit[vy][vx] != 0)
53 {
54 cout << visit[vy][vx] - 1<<" step(s) before a loop of "<<step -visit[vy][vx] + 1<< " step(s)" << endl;
55 break;
56 }
57 else
58 visit[vy][vx] = ++step;
59 }
60 }
61 return 0;
62 }

 

posted on 2010-10-31 10:21  ltang  阅读(113)  评论(0)    收藏  举报

导航