-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10116.cpp
84 lines (63 loc) · 1.53 KB
/
uva-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//uva 10116
//Robot Motion
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(void)
{
int n, m, start;
while (true) {
cin >> m >> n >> start;
if (n == 0 && m == 0 && start == 0)
break;
vector <string> arr(m);
for (int i = 0; i < m; ++i)
cin >> arr[i];
pair <int, int> current;
current.first = 0;
current.second = start - 1;
int steps = 0;
bool infiniteLoop = 0;
vector < vector <bool> > visited(m, vector <bool> (n, false));
while (current.first >= 0 && current.second >= 0 && current.first < m && current.second < n) {
int x = current.first;
int y = current.second;
if (visited[x][y]){
infiniteLoop = 1;
break;
}
if (arr[x][y] == 'N')
current.first -= 1;
else if (arr[x][y] == 'S')
current.first += 1;
else if (arr[x][y] == 'W')
current.second -= 1;
else if (arr[x][y] == 'E')
current.second += 1;
visited[x][y] = 1;
++steps;
}
if (infiniteLoop){
int repeatLength = 0;
pair <int, int> tmp = current;
do {
int x = tmp.first;
int y = tmp.second;
if (arr[x][y] == 'N')
tmp.first -= 1;
else if (arr[x][y] == 'S')
tmp.first += 1;
else if (arr[x][y] == 'W')
tmp.second -= 1;
else if (arr[x][y] == 'E')
tmp.second += 1;
++repeatLength;
}while(tmp.first != current.first || tmp.second != current.second);
cout << steps - repeatLength << " step(s) before a loop of " << repeatLength << " step(s)" << endl;
}
else
cout << steps << " step(s) to exit" << endl;
}
return 0;
}