The Worm Turns

FJNU.1823

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.

Source
East Central North America 2001, Practice

My Program

#include < iostream >
#define  N 100
#define  M 50
using   namespace  std;

int  main()
{
    
int ox[N+20],oy[N+20];
    
char str[N];
    
int n,i,x,y,flag;
    
while(cin>>n)
    
{
        
if(n==0)
            
break;
        flag
=0;
        
int map[M][M]={0};
        
for(i=10;i<30;i++)
            map[
24][i]=1;
        
for(i=0;i<n;i++)
            cin
>>str[i];
        
for(i=0;i<20;i++)
        
{
            ox[i]
=25;
            oy[i]
=11+i;
        }

        x
=ox[19];
        y
=oy[19];
        
for(i=0;i<n;i++)
        
{
            
switch(str[i])
            
{
            
case 'E':
                y
++;
                
break;
            
case 'W':
                y
--;
                
break;
            
case 'N':
                x
--;
                
break;
            
case 'S':
                x
++;
                
break;
            }

            
if(x<=0||x>50||y<=0||y>50)
            
{
                flag
=1;
                cout
<<"The worm ran off the board on move "<<i+1<<"."<<endl;
                
break;
            }

            map[(ox[i]
-1)][(oy[i]-1)]=0;
            
if(map[x-1][y-1]==1)
            
{
                flag
=1;
                cout
<<"The worm ran into itself on move "<<i+1<<"."<<endl;
                
break;
            }

            map[x
-1][y-1]=1;
            ox[i
+20]=x;
            oy[i
+20]=y;
        }

        
if(flag==0)
            cout
<<"The worm successfully made all "<<n<<" moves."<<endl;
    }

    
return 0;
}

 YOYO's Note:
简化的贪食蛇,模拟~
这里用一个二维数组MAP记录当前坐标上是否有蛇身,另外开两数组分别记录蛇曾访问过的x、y,
按顺序抹掉蛇尾添加蛇头,判定好状态就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值