模拟,没用队列,60pts,求大佬
#include<bits/stdc++.h>
using namespace std;
int n;
int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
string a;
int xt,yt,xw,yw;
bool vis[55][55];
int main(){
while(cin>>n){
memset(vis,0,sizeof(vis));
int m[55][55][3];
if(n==0) break;
cin>>a;
xt=25,yt=30;
xw=25,yw=11;
for(int i=11;i<=30;i++){
vis[25][i]=1;
if(i==30) continue;
m[25][i][1]=25;
m[25][i][2]=i+1;
}
bool f=0;
for(int i=0;i<a.size();i++){
vis[xw][yw]=0;
xw=m[xw][yw][1];
yw=m[xw][yw][2];
if(a[i]=='N'){
m[xt][yt][1]=xt-1;
m[xt][yt][2]=yt;
xt--;
if(vis[xt][yt]==1){
//cout<<1111<<endl;
cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
// The worm ran into itself on move m .
f=1;
break;
}
vis[xt][yt]=1;
}
if(a[i]=='S'){
m[xt][yt][1]=xt+1;
m[xt][yt][2]=yt;
xt++;
if(vis[xt][yt]==1){
cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
// The worm ran into itself on move m .
f=1;
break;
}
vis[xt][yt]=1;
}
if(a[i]=='W'){
m[xt][yt][1]=xt;
m[xt][yt][2]=yt-1;
yt--;
if(vis[xt][yt]==1){
cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
// The worm ran into itself on move m .
f=1;
break;
}
vis[xt][yt]=1;
}
if(a[i]=='E'){
m[xt][yt][1]=xt;
m[xt][yt][2]=yt+1;
yt++;
if(vis[xt][yt]==1){
cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;
// The worm ran into itself on move m .
f=1;
break;
}
vis[xt][yt]=1;
}
//cout<<xt<<" "<<yt<<" "<<xw<<" "<<yw<<endl;
if(!(xt>=1&&xt<=50&&yt>=1&&yt<=50)){
cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;
f=1;
break;
}
}
if(f==0){
cout<<"The worm successfully made all "<<n<<" moves."<<endl;
// The worm successfully made all m moves.
}
}
return 0;
}