Robot Motion--ZOJ 1708
1、解题思路:简单模拟。
2、注意事项:注意数组边界。
3、实现代码:
1 #include <iostream>
2 using namespace std;
3 #define N 10
4
5 int data[N][N];
6 int rn,cn;
7
8 void Init()
9 {
10 char ch;
11 for(int i=0;i<rn;i++)
12 {
13 for( int j=0;j<cn;j++ )
14 {
15 cin >> ch;
16 switch(ch)
17 {
18 case 'N':
19 data[i][j] = -1;
20 break;
21 case 'S':
22 data[i][j] = -2;
23 break;
24 case 'W':
25 data[i][j] = -3;
26 break;
27 case 'E':
28 data[i][j] = -4;
29 break;
30 }
31 }
32 }
33 }
34
35 void Move(int row,int col)
36 {
37 int place = 1;
38 bool notExit = true;
39 while(notExit && data[row][col]<0)
40 {
41 int d = data[row][col];
42 data[row][col] = place;
43 switch(d)
44 {
45 case -1:
46 row--;
47 break;
48 case -2:
49 row++;
50 break;
51 case -3:
52 col--;
53 break;
54 case -4:
55 col++;
56 break;
57 }
58 place++;
59 notExit = (row>=0)&&(row<rn)&&(col>=0)&&(col<cn);
60 }
61 if( !notExit )
62 cout << place-1 <<" step(s) to exit" << endl;
63 else
64 cout << data[row][col]-1 << " step(s) before a loop of "
65 << place-data[row][col] << " step(s)"
66 << endl;
67 }
68
69 int main()
70 {
71 int start;
72 while(cin>>rn>>cn>>start && rn!=0)
73 {
74 Init();
75 Move(0,start-1);
76 }
77 return 1;
78 }