forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1056The Worm Turns.cpp
More file actions
98 lines (93 loc) · 1.87 KB
/
1056The Worm Turns.cpp
File metadata and controls
98 lines (93 loc) · 1.87 KB
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
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
using namespace std;
int main(void){
int n,i,j,t;
int x[20],y[20];
while(cin>>n&&n!=0){
char* a=new char[n+1];
cin>>a;
int num[51][51]={0};
for(i=0;i<20;i++){
x[i]=30-i;
y[i]=25;
num[x[i]][y[i]]=1;
}
for(j=0;j<n;j++){
if(a[j]=='E'){
num[x[19]][y[19]]=0;
if(x[0]+1>50){
cout<<"The worm ran off the board on move "<<j+1<<"."<<endl;
break;
}
if(num[x[0]+1][y[0]]==1){
cout<<"The worm ran into itself on move "<<j+1<<"."<<endl;
break;
}
for(t=19;t>=1;t--){
x[t]=x[t-1];
y[t]=y[t-1];
}
x[0]=x[0]+1;
y[0]=y[0];
num[x[0]][y[0]]=1;
}
if(a[j]=='S'){
num[x[19]][y[19]]=0;
if(y[0]+1>50){
cout<<"The worm ran off the board on move "<<j+1<<"."<<endl;
break;
}
if(num[x[0]][y[0]+1]==1){
cout<<"The worm ran into itself on move "<<j+1<<"."<<endl;
break;
}
for(t=19;t>=1;t--){
x[t]=x[t-1];
y[t]=y[t-1];
}
x[0]=x[0];
y[0]=y[0]+1;
num[x[0]][y[0]]=1;
}
if(a[j]=='W'){
num[x[19]][y[19]]=0;
if(x[0]-1<1){
cout<<"The worm ran off the board on move "<<j+1<<"."<<endl;
break;
}
if(num[x[0]-1][y[0]]==1){
cout<<"The worm ran into itself on move "<<j+1<<"."<<endl;
break;
}
for(t=19;t>=1;t--){
x[t]=x[t-1];
y[t]=y[t-1];
}
x[0]=x[0]-1;
y[0]=y[0];
num[x[0]][y[0]]=1;
}
if(a[j]=='N'){
num[x[19]][y[19]]=0;
if(y[0]-1<1){
cout<<"The worm ran off the board on move "<<j+1<<"."<<endl;
break;
}
if(num[x[0]][y[0]-1]==1){
cout<<"The worm ran into itself on move "<<j+1<<"."<<endl;
break;
}
for(t=19;t>=1;t--){
x[t]=x[t-1];
y[t]=y[t-1];
}
x[0]=x[0];
y[0]=y[0]-1;
num[x[0]][y[0]]=1;
}
if(j==n-1) cout<<"The worm successfully made all "<<j+1<<" moves."<<endl;
}
delete []a;
}
return 0;
}