#include<bits/stdc++.h>
using namespace std;
int len,x[1000],y[1000],way,cnt;
bool mp[51][51],flag;
string a;
void init()
{
flag=false;
memset(mp,false,sizeof(mp));
for(int i=11;i<=30;i++)
{
x[i-10]=25;
y[i-10]=i;
mp[x[i]][y[i]]=true;
}
cnt=20;
}
//1 up
//2 down
//3 left
//4 right
void f(int w,int step)
{
if(way==1)
{
x[cnt]=x[cnt-1]-1;
y[cnt]=y[cnt-1];
}
if(way==2)
{
x[cnt]=x[cnt-1]+1;
y[cnt]=y[cnt-1];
}
if(way==3)
{
x[cnt]=x[cnt-1];
y[cnt]=y[cnt-1]-1;
}
if(way==4)
{
x[cnt]=x[cnt-1];
y[cnt]=y[cnt-1]+1;
}
mp[x[cnt-20+1]][y[cnt-20+1]]=false;
if(mp[x[cnt]][y[cnt]]==true)
{
printf("The worm ran into inself on move %d.\n",step);
flag=true;
}
else if(x[cnt]>50||y[cnt]>50||x[cnt]<1||y[cnt]<1)
{
printf("The worm ran off the board on move %d.\n",step);
flag=true;
}
else mp[x[cnt]][y[cnt]]=true;
}
int main()
{
// freopen("worm.in","r",stdin);
// freopen("worm.out","w",stdout);
while(true)
{
cin>>len;
if(len==0) break;
cin>>a;
init();
for(int k=0;k<=len;k++)
{
if(a[k]=='N') way=1;
if(a[k]=='S') way=2;
if(a[k]=='W') way=3;
if(a[k]=='E') way=4;
cnt++;
f(way,k+1);
if(flag==true) break;
}
if(flag==true) continue;
printf("The worm successfully made all %d moves.\n",len);
}
return 0;
}
样例可以过,不知道哪里出了问题