TOJ 1191. The Worm Turns

191.   The Worm Turns
Time Limit: 1.0 Seconds   Memory Limit: 65536K Total Runs: 5465   Accepted Runs: 1774



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.

题目大意:整个游戏棋盘是50*50大小的,左上角在(1,1),贪吃蛇由20个节点组成,头部位置在(25,30),水平延展到(25,11),可以有四个运动方向:东,西,南,北。题目就是给你一个运动序列,判断最终结果是下面3种情况的哪一种:1)正常。2)头撞到自己身体。3)出界。

这是一题模拟题,简单的贪吃蛇游戏,实现一些基本的功能。
这题有注意点: (1) 头碰到尾时需要注意,即移动的时候先移尾部再移头部.
#include <iostream>

#include <string>

using namespace std;

//贪吃蛇节点

struct WNode

{

    int x;//行号

    int y;//列号

};

int main()

{

    string moves;//移动序列

    WNode worm[20];//贪吃蛇

    int n,i,j;

    while(cin>>n&&n!=0)

    {

        //从头部到尾部初始化贪吃蛇

        for(i=0;i<20;++i)

        {

            worm[i].x = 25;//起始行在行

            worm[i].y = 30-i;//起始所在列

        }

        cin>>moves;//输入移动序列

        for (i=0;i<n;++i)

        {

            //贪吃蛇中其他节点移动到前一个节点位置上

            for(j=19;j>0;--j)

            {

                worm[j].x=worm[j-1].x;

                worm[j].y=worm[j-1].y;

            }

            //移动头部

            if (moves[i]=='N')

            {//向北

                worm[0].x -= 1;

            }

            else if (moves[i]=='S')

            {//向南

                worm[0].x += 1;

            }

            else if (moves[i]=='W')

            {//向西

                worm[0].y -= 1;

            }

            else if (moves[i]=='E')

            {//向东

                worm[0].y += 1;

            }

            //判断是否出界

            if(worm[0].x>50||worm[0].y>50||worm[0].x<1||worm[0].y<1)

            {

                cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;

                break;

            }    

            //判断是否撞到自己身体了

            for(j=1;j<20;++j)

            {

                //头部节点撞到其他节点

                if(worm[0].x==worm[j].x&&worm[0].y==worm[j].y)

                {

                    cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;

                    break;

                }

            }

            if(j!=20) break;//发生了碰撞,不能继续运动了

        }

        if(i==n) 

            cout<<"The worm successfully made all "<<n<<" moves."<<endl;

    }

    return 0;

}
原文地址:https://www.cnblogs.com/wft1990/p/6168774.html