-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10116.cpp
executable file
·69 lines (60 loc) · 1.34 KB
/
10116.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Problem: Robot Motion UVa 10116
Programmer: Md. Mahmud Ahsan
Description: Graph
Compiled: Visual C++ 7.0
Date: 17-08-06
*/
#include <iostream>
#include <string>
using namespace std;
const int MX = 100;
char grid[MX][MX];
int gridValue[MX][MX];
void init(int row, int col){
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
gridValue[i][j] = -1;
}
int main(){
//freopen("input.txt", "r", stdin);
int row, col, pos;
int a, b, path, exit, b4Loop;
char ch;
bool found;
while (cin >> row >> col >> pos){
if (!row && !col && !pos) break;
for (int i = 0; i < row; ++i)
cin >> grid[i];
init(row, col);
found = false;
a = 0;
b = --pos;
path = 0;
while (true){
ch = grid[a][b];
gridValue[a][b] = path++;
switch (ch){
case 'N': --a; break;
case 'S': ++a; break;
case 'E': ++b; break;
case 'W': --b; break;
}
if (a < 0 || a >= row || b < 0 || b >= col){
found = true;
exit = path;
break;
}
else if (gridValue[a][b] != -1){
found = false;
exit = path - gridValue[a][b];
b4Loop = gridValue[a][b];
break;
}
}
if (found)
cout << exit << " step(s) to exit" << endl;
else
cout << b4Loop << " step(s) before a loop of " << exit << " step(s)" << endl;
}
return 0;
}