ZOJ Problem Set - 1056

来源:互联网 发布:策略交易软件 编辑:程序博客网 时间:2024/06/03 21:41

【【模拟题】】

 

题目

The Worm Turns

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

题意说明

        蠕虫游戏是比较经典的掌机游戏,简单的版本是对于一条在格子界面里乱窜的蠕虫,玩家要控制它的移动方向以免它碰到自己的身体或墙壁。题目模拟了简单的蠕虫游戏给出一个50×50的格子矩阵,以及其中长度为20个格子的蠕虫头、尾坐标,初始时虫身为虫头、尾直连。要求根据输入的运动方向步骤字串('E''W''S''N'表示分别向东西南北移动),输出蠕虫的移动结果(某一步虫头碰到了自己的身体或者墙壁,或者安全地移动完了所有步骤)。

图1

解答

(一)分析:制定50×50二维BOOL数组bord代表蠕虫爬动的格子矩阵,数组元素bord(i,j)表示矩阵中横坐标为j、纵坐标为i的格子(i增加表示纵坐标往上,j增加表示横坐标往右),格子值为true表示该格子为蠕虫身体,为false表示为空格,bord(0,0)为矩阵坐标系原点。对于每一次移动,格子矩阵中只有2个格子的状态会发生变化,一个是虫头将要移至的格子状态将要变为true,一个是虫尾移走后原先所处的格子状态变为false,故程序设计的模拟过程只需考虑虫头和虫尾的移动方向变化,即每次移动步骤根据移动方向确定虫头、虫尾哪一方向面的格子状态需要变化。需要注意的是,虫头牵引着整个虫身(包括虫尾)的移动,所以每次虫头的方向和输入给出的每次移动方向相同,而虫尾的移动方向则要根据当前所处的位置判定,如图1某轮的移动要求突然向南,则虫头向南,而虫尾原先向东,此时它的东面还有虫身在移动,故虫尾仍向东直到其跟随虫身移动到南北走向后则向北移动。最终即判定在移动过程中虫头将要移至的格子是否为虫身或越界,否则表示安全移动完成所有输入步骤要求。

(二)代码:

#include<iostream>using namespace std;int main(){bool board[50][50];//模拟蠕虫爬行矩阵格子,格子值为true表示该格为蠕虫身体,false为空格int hi,hj,ti,tj;//蠕虫头格子坐标(hi,hj)、尾格子坐标(ti,tj)int n;//移动方向串长int i,j;char moves[100],td;//moves表示移动方向串。td表示虫尾的运动方向while(cin>>n){if(n==0)break;cin>>moves;        hi=24;hj=29;ti=24;tj=10;for(i=0;i<50;i++)for(j=0;j<50;j++)board[i][j]=false;for(j=10;j<=29;j++)board[24][j]=true;td='E';for(i=0;i<n;i++){//虫尾准备移走,腾空原先所处格子board[ti][tj]=false;//移动虫头方向if(moves[i]=='E'){hj++;}else if(moves[i]=='W'){hj--;}else if(moves[i]=='N'){hi++;}else{hi--;}//虫尾随着虫头准备向东,而虫尾的东面是空格,则虫尾向南或向北if(td=='E'&&board[ti][tj+1]==false){if(board[ti-1][tj]==true)//虫尾南面是虫身,则虫尾向南{ti--;td='S';}else if(board[ti+1][tj]==true)//虫尾北面是虫身,则虫尾向北{ti++;td='N';}}//虫尾随着虫头准备向西,而虫尾的西面是空格,则虫尾向南或向北else if(td=='W'&&board[ti][tj-1]==false){if(board[ti-1][tj]==true)//虫尾南面是虫身,则虫尾向南{ti--;td='S';}else if(board[ti+1][tj]==true)//虫尾北面是虫身,则虫尾向北{ti++;td='N';}}//虫尾随着虫头准备向北,而虫尾的北面是空格,则虫尾向西或向东else if(td=='N'&&board[ti+1][tj]==false){if(board[ti][tj-1]==true)//虫尾西面是虫身,则虫尾向西{tj--;td='W';}else if(board[ti][tj+1]==true)//虫尾东面是虫身,则虫尾向东{tj++;td='E';}}//虫尾随着虫头准备向南,而虫尾的南面是空格,则虫尾向西或向东else if(td=='S'&&board[ti-1][tj]==false){if(board[ti][tj-1]==true)//虫尾西面是虫身,则虫尾向西{tj--;td='W';}else if(board[ti][tj+1]==true)//虫尾东面是虫身,则虫尾向东{tj++;td='E';}}//虫尾随着虫头准备向东西南北,而虫尾的相应东西南北面是虫身,则虫尾和虫头移动方向相同else{if(td=='E') tj++;else if(td=='W') tj--;else if(td=='S') ti--;else ti++;}//虫头碰撞到虫身if(board[hi][hj]==true){cout<<"The worm ran into itself on move "<<i+1<<"."<<endl;break;}//虫头移动到矩阵外界if(hi<0||hi>=50||hj<0||hj>=50){cout<<"The worm ran off the board on move "<<i+1<<"."<<endl;break;}//虫头移动到下一个空格board[hi][hj]=true;}if(i>=n)cout<<"The worm successfully made all "<<i<<" moves."<<endl;}return 0;}//Accepted


 (解于2009/10)

 

原创粉丝点击