| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 478 人关注过本帖
标题:哪里出错了 麻烦大神帮忙
只看楼主 加入收藏
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
结帖率:77.78%
收藏
已结贴  问题点数:20 回复次数:6 
哪里出错了 麻烦大神帮忙
#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.

搜索更多相关主题的帖子: include 
2012-12-05 00:47
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
收藏
得分:0 
难道break不是iostream的?
2012-12-05 00:54
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:5 
break是返回,不要任何库支持

是不是你m输错了。

[ 本帖最后由 mayuebo 于 2012-12-5 06:19 编辑 ]

成功贵在坚持
2012-12-05 06:17
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:5 
运行没报错

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-12-05 11:29
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
收藏
得分:0 
回复 4楼 mmmmmmmmmmmm
那个无法达到题目要求,输0停止,不输循环
2012-12-05 13:19
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:0 
while(cin >> num && num != 0)

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-12-05 14:48
维555我
Rank: 2
等 级:论坛游民
帖 子:28
专家分:38
注 册:2012-12-5
收藏
得分:5 
好庞大。。。。
2012-12-05 15:04
快速回复:哪里出错了 麻烦大神帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053202 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved