Description
Worm is an old computer game. There are many versions, but all involve maneuvering a "worm" around the screen, trying to avoid running the worm into itself or an obstacle.
We'll simulate a very simplified version here. The game will be played on a 50 x 50 board, numbered so that the square at the upper left is numbered (1, 1). The worm is initially a string of 20 connected squares. Connected squares are adjacent horizontally or vertically. The worm starts stretched out horizontally in positions (25, 11) through (25, 30), with the head of the worm at (25, 30). The worm can move either East (E), West (W), North (N) or South (S), but will never move back on itself. So, in the initial position, a W move is not possible. Thus the only two squares occupied by the worm that change in any move are its head and tail. Note that the head of the worm can move to the square just vacated by the worm's tail.
You will be given a series of moves and will simulate the moves until either the worm runs into itself, the worm runs off the board, or the worm successfully negotiates its list of moves. In the first two cases you should ignore the remaining moves in the list.
Input
There will be multiple problems instances. The input for each problem instance will be on two lines. The first line is an integer n (<100) indicating the number of moves to follow. (A value of n = 0 indicates end of input.) The next line contains n characters (either E, W, N or S), with no spaces separating the letters, indicating the sequence of moves.
Output
Generate one line of output for each problem instance. The output line should be one of the follow three:
The worm ran into itself on move m.
The worm ran off the board on move m.
The worm successfully made all m moves.
Where m is for you to determine and the first move is move 1.
Sample Input
18
NWWWWWWWWWWSESSSWS
20
SSSWWNENNNNNWWWWSSSS
30
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
13
SWWWWWWWWWNEE
0
Sample Output
The worm successfully made all 18 moves.
The worm ran into itself on move 9.
The worm ran off the board on move 21.
The worm successfully made all 13 moves.
题意:一个小虫子在50X50的格子上,身体长20个单位,给出N个行走方向,判断在行走中是否碰到自己的身体,是否出边界,是否可以安全行走完。
代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
char s[105];
int a[55][55],book[105],i,j,tx,ty,hx,hy,n;
while(scanf("%d",&n),n!=0)
{
getchar();
memset(a,0,sizeof(a));
memset(book,0,sizeof(book));
for(i=1;i<=n;i++)
scanf("%c",&s[i]);
for(i=11;i<=30;i++)
a[25][i]=1;//标记路径 1为身体,0为空格
tx=25;ty=11;
hx=25;hy=30;
for(i=1;i<=19;i++)
book[i]=4;//标记方向4,3,2,1分别表示东,北,西,南;
for(i=1;i<=n;i++)
{
if(s[i]=='E')//向东行走时
{
a[tx][ty]=0;//尾部标记取消
if(a[hx][hy+1]==1)//向东走时是否碰到身体
{
printf("The worm ran into itself on move %d.\n",i);
break;
}
hy=hy+1;//头部列坐标加一
a[hx][hy]=1;//标记
if(book[i]==4)//向东行走,尾部坐标变化
ty=ty+1;
if(book[i]==3)//向北行走,尾部坐标变化
tx=tx-1;
if(book[i]==2)//向西行走,尾部坐标变化
ty=ty-1;
if(book[i]==1)//向南行走,尾部坐标变化
tx=tx+1;
book[i+19]=4;//头部行走方向与19步后尾部的方向一致
}
if(s[i]=='S')//与上面想法一样,只不过方向变为南
{
a[tx][ty]=0;
if(a[hx+1][hy]==1)
{
printf("The worm ran into itself on move %d.\n",i);
break;
}
hx=hx+1;
a[hx][hy]=1;
if(book[i]==4)
ty=ty+1;
if(book[i]==3)
tx=tx-1;
if(book[i]==2)
ty=ty-1;
if(book[i]==1)
tx=tx+1;
book[i+19]=1;
}
if(s[i]=='W')//与上面想法一样,只不过方向变为西
{
a[tx][ty]=0;
if(a[hx][hy-1]==1)
{
printf("The worm ran into itself on move %d.\n",i);
break;
}
hy=hy-1;
a[hx][hy]=1;
if(book[i]==4)
ty=ty+1;
if(book[i]==3)
tx=tx-1;
if(book[i]==2)
ty=ty-1;
if(book[i]==1)
tx=tx+1;
book[i+19]=2;
}
if(s[i]=='N')//与上面想法一样,只不过方向变为北
{
a[tx][ty]=0;
if(a[hx-1][hy]==1)
{
printf("The worm ran into itself on move %d.\n",i);
break;
}
hx--;
a[hx][hy]=1;
if(book[i]==4)
ty=ty+1;
if(book[i]==3)
tx=tx-1;
if(book[i]==2)
ty=ty-1;
if(book[i]==1)
tx=tx+1;
book[i+19]=3;
}
if(hx<1||hx>50||hy<1||hy>50)//超出边界范围
{
printf("The worm ran off the board on move %d.\n",i);
break;
}
}
if(i==n+1)
printf("The worm successfully made all %d moves.\n",n);
}
return 0;
}