-
Notifications
You must be signed in to change notification settings - Fork 1
/
P10116.cpp
54 lines (51 loc) · 1.13 KB
/
P10116.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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
int H, W, startX, visited[100], walkX[100], walkY[100];
string s;
while(true) {
cin >> H >> W >> startX; --startX;
if(H == 0 && W == 0 && startX == -1)
return 0;
memset(visited, 0, 100*sizeof(int));
for(int y = 0; y < H; ++y) {
cin >> s;
for(int x = 0; x < W; ++x) {
switch(s[x]) {
case 'N':
walkX[10*y+x] = 0;
walkY[10*y+x] = -1;
break;
case 'S':
walkX[10*y+x] = 0;
walkY[10*y+x] = 1;
break;
case 'E':
walkX[10*y+x] = 1;
walkY[10*y+x] = 0;
break;
case 'W':
walkX[10*y+x] = -1;
walkY[10*y+x] = 0;
break;
}
}
}
int X = startX;
int Y = 0;
int time = 1;
while(X >= 0 && Y >= 0 && X < W && Y < H && visited[10*Y+X] == 0) {
visited[10*Y+X] = time;
int x = X;
X += walkX[10*Y+x];
Y += walkY[10*Y+x];
++time;
}
if(X >= 0 && Y >= 0 && X < W && Y < H)
cout << visited[10*Y+X]-1 << " step(s) before a loop of " << time-visited[10*Y+X] << " step(s)" << endl;
else
cout << time-1 << " step(s) to exit" << endl;
}
}