浙大在线评测 1056 The Worm Turns

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Problem:

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.

Solution:

// 声明:本代码仅供学习之用,请不要作为个人的成绩提交。

// http://blog.csdn.net/mskia

// email: bitrain@hotmail.com

#include <iostream>

#include <string>

#include <vector>

using namespace std;

vector< int > row( 50 , 0 );

int main( void ) {

for ( ; ; ) {

int dataNum;

cin >> dataNum;

if ( dataNum == 0 ) {

break;

}

vector< vector< int > > box( 50 , row );

vector< int > &worm = box[ 24 ];

for ( int i = 10; i < 30; ++i ) {

worm[ i ] = 2;

}

int headX = 24 , headY = 29;

int tailX = 24 , tailY = 10;

string str;

cin >> str;

dataNum = str.length( );

for ( int i = 1; i <= dataNum; ++i ) {

int direct = box[ tailX ][ tailY ];

box[ tailX ][ tailY ] = 0;

switch ( direct ) {

case 1: --tailX; break;

case 2: ++tailY; break;

case 3: ++tailX; break;

case 4: --tailY; break;

}

switch ( str[ i - 1 ] ) {

case 'N':

box[ headX ][ headY ] = 1;

if ( --headX < 0 ) {

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

goto ENDINPUT;

}

if ( box[ headX ][ headY ] != 0 ) {

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

goto ENDINPUT;

}

box[ headX ][ headY ] = 1;

break;

case 'E':

box[ headX ][ headY ] = 2;

if ( ++headY > 49 ) {

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

goto ENDINPUT;

}

if ( box[ headX ][ headY ] != 0 ) {

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

goto ENDINPUT;

}

box[ headX ][ headY ] = 2;

break;

case 'S':

box[ headX ][ headY ] = 3;

if ( ++headX > 49 ) {

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

goto ENDINPUT;

}

if ( box[ headX ][ headY ] != 0 ) {

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

goto ENDINPUT;

}

box[ headX ][ headY ] = 3;

break;

case 'W':

box[ headX ][ headY ] = 4;

if ( --headY < 0 ) {

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

goto ENDINPUT;

}

if ( box[ headX ][ headY ] != 0 ) {

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

goto ENDINPUT;

}

box[ headX ][ headY ] = 4;

break;

}

}

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

ENDINPUT:;

}

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航