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[];//贪吃蛇

    int n,i,j;

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

    {

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

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

        {

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

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

        }

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

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

        {

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

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

            {

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

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

            }

            //移动头部

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

            {//向北

                worm[].x -= ;

            }

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

            {//向南

                worm[].x += ;

            }

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

            {//向西

                worm[].y -= ;

            }

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

            {//向东

                worm[].y += ;

            }

            //判断是否出界

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

            {

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

                break;

            }    

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

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

            {

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

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

                {

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

                    break;

                }

            }

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

        }

        if(i==n) 

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

    }

    return ;

}

TOJ 1191. The Worm Turns的更多相关文章

  1. TJU ACM-ICPC Online Judge—1191 The Worm Turns

    B - The Worm Turns Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Su ...

  2. The Worm Turns

    The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. HDU 2782 The Worm Turns (DFS)

    Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...

  4. ZOJ 1056 The Worm Turns

    原题链接 题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活. 解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标.每读入一条指令,判断其是否越界 ...

  5. 【HDOJ】2782 The Worm Turns

    DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...

  6. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  7. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  8. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  9. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

随机推荐

  1. quicksort

    快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...

  2. vert.x学习(四),使用模板解析器ClassLoaderTemplateResolver

    在vert.x中使用模板解析,可以为我们带来很多方便.我这里学习了一下ClassLoaderTemplateResolver的简单使用.这次工程配置与上篇一样,不需要做任何多的配置.直接编写代码就可以 ...

  3. jquery on 绑定事件

    jquery on 绑定事件 1. 多个选择器绑定一个事件 2. 多个事件绑定一个函数 3. 一个选择器绑定多个事件,有两种写法: 或者 on只绑定一次事件,绑定父元素,防止初始化时数据未加载,绑定出 ...

  4. Java中Properties类知识的总结

    一.Properties类与配置文件 注意:是一个Map集合,该集合中的键值对都是字符串.该集合通常用于对键值对形式的配置文件进行操作. 配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后 ...

  5. 浅谈MySQL数据类型

    MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 一.数值类型 MySQL支持所有标 ...

  6. Excel筛选之后如何下拉递增

    1.痛点 Excel表格,通过筛选了之后,再想统计行数,通过单纯的拖动或者填充排序啥的,都无法做到排序或行数递增: 2.解决方案 发现了个excel的公式可以完美解决该问题,赞个,找的好辛苦. 3.大 ...

  7. delphi 弹出选择目录窗口

    if not SelectDirectory( '请选择输出文件路径','/',directory) then begin Exit; end; 使用SelectDirectory函数注意要在use下 ...

  8. 如果空间不够的话,iOS发生这样的错误

    2016-12-16 10:24:50.945 gpxj[2634:21323] Simulator user has requested new graphics quality: 10 2016- ...

  9. windows10搭建django1.10.3+Apache2.4

    很多教程都是在linux上搭建,windows上似乎天生不太适合,但是我还是愿意试试这个坑. 首先 交代一下自己的环境 python3.5.2 64位 django 1.10.3 apache 2.4 ...

  10. 新上市Lighthouse专用芯片TS3633规格介绍

    背景介绍 Valve 有远大的愿景.它决心要把 SteamVR 追踪系统推向世界,从虚拟现实里的空间定位,到机器人领域,Valve 想为各种环境下的跟踪应用提供支持. 上个月,Valve 方面宣布会把 ...