注册 登录
编程论坛 C++教室

哪里出错了 麻烦大神帮忙

bbb222 发布于 2012-12-05 00:47, 478 次点击
#include <iostream>
using namespace std;

int main()
{
    int board[50][50];
    int x=25,y=30;
    int num;
    char ch;
    int N = 1;
    while(cin >> num)
    {

        for(int i=0; i<50; i++)
        {
            for(int j=0; j<50; j++)
                board[i][j]=0;
        }
        for(y=11; y<=30; y++)
            board[25][y]=y-10;
        x=25;
        y=30;

        if(num == 0)
            break;
        for(int k=0; k<num; k++)
        {
            cin>>ch;
            if(ch=='E')
                y+=1;
            if(ch=='N')
                x-=1;
            if(ch=='S')
                x+=1;
            if(ch=='W')
                y-=1;

            if(x<0||x>50||y<0||y>50)
            {
                cout<<"The worm ran off the board on move"<<" "<<k+1<<"."<< endl;
                break;
            }

            if(board[x][y]>0)
            {
                cout<<"The worm ran into itself on move"<<" "<<k+1<<"."<<endl;
                break;

            }
            if(board[x][y]==0)
            {
                for(int a=0; a<50; a++)
                {
                    for(int b=0; b<50; b++)
                    {
                        if(board[a][b]>0){
                            board[a][b]-=1;

                        }
                    }

                }
                board[x][y]=20;
                if(k==num-1)
                {
                    cout<<"The worm successfully made all"<<" "<<num<<" moves."<<endl;
                    break;
                }

            }
        }
    }
    return 0;
}
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.

6 回复
#2
bbb2222012-12-05 00:54
难道break不是iostream的?
#3
mayuebo2012-12-05 06:17
break是返回,不要任何库支持

是不是你m输错了。

[ 本帖最后由 mayuebo 于 2012-12-5 06:19 编辑 ]
#4
mmmmmmmmmmmm2012-12-05 11:29
运行没报错
#5
bbb2222012-12-05 13:19
回复 4楼 mmmmmmmmmmmm
那个无法达到题目要求,输0停止,不输循环
#6
mmmmmmmmmmmm2012-12-05 14:48
while(cin >> num && num != 0)
#7
维555我2012-12-05 15:04
好庞大。。。。
1