其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
浙大在线评测 1056 The Worm Turns

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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: [email protected]

#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;
}




相关文章

相关软件