-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10116.cpp
78 lines (75 loc) · 1.4 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
70
71
72
73
74
75
76
77
78
/*
10116 - Robot Motion
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
/// freopen("input.txt", "r", stdin);
/// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,s;
while(cin>>n>>m>>s)
{
if(n==0 && m==0 && s==0)
break;
char mat[20][20];
int vis[20][20];
for(int i=0; i<n; i++)
{
cin>>mat[i];
}
memset(vis,-1,sizeof(vis));
int x=0,y=s-1,step=0;
vis[x][y]=0;
while(1)
{
step++;
if(mat[x][y]=='N')
{
x--;
}
else if(mat[x][y]=='S')
{
x++;
}
else if(mat[x][y]=='E')
{
y++;
}
else
{
y--;
}
if(x<0 || y<0 || x>=n || y>=m)
{
cout<<step<<" step(s) to exit"<<endl;
break;
}
if(vis[x][y]!=-1)
{
cout<<vis[x][y]<<" step(s) before a loop of "<<step-vis[x][y]<<" step(s)"<<endl;
break;
}
vis[x][y]=step;
}
}
return 0;
}
/*
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
*/