qq_36890193 2016-12-01 06:16 采纳率: 0%
浏览 1001

zoj 1056 一道简单的acm,测试数据对,就是不通过 用的c++

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.

初始位置头是24,30,尾是24,11
以下是WA的代码
#include
using namespace std;

int main()
{
int orders;
while(cin >> orders && orders!= 0)
{
bool s2[50][50];
for(int i = 0; i < 50; i++)
{
for(int j = 0; j < 50; j++)
{
s2[i][j] = true;
}
}
int a = 24, b = 29, c = 24, d = 10;
s2[a][b] = true;

    for(int i = 10; i < 29; i++)
    {
        s2[24][i] = false;
    }

    string ord;
    cin >> ord;
    for(int i = 0; i < orders; i++)
    {
        if(ord[i] == 'N')
        {s2[a][b] = false;
            a--;}
        else if(ord[i] == 'S')
        {s2[a][b] = false;
            a++;}
        else if(ord[i] == 'E')
        {s2[a][b] = false;
            b++;}
        else if(ord[i] == 'W')
        {s2[a][b] = false;
            b--;}

                    //处理蛇尾
                    if(i < 19 && i != orders - 1)
                    {
                      s2[c][d] = true;
                      d++;
                    }
                    else if(i >= 19 && i != orders - 1)
                    {
                      if(ord[i - 20] == 'N')
                      {
                          s2[c][d] = true;
                          c--;
                      }
                      else if(ord[i - 20] == 'S')
                      {
                          s2[c][d] = true;
                          c++;
                      }
                      else if(ord[i - 20] == 'E')
                      {
                          s2[c][d] = true;
                          d++;
                      }
                      else if(ord[i - 20] == 'W')
                      {
                          s2[c][d] = true;
                          d--;
                      }
                    }
                    else if(i == orders - 1)
                    {
                      cout << "The worm successfully made all " << orders << " moves." <<endl;
                      break;
                    }


            if(a < 0 || a > 49 || b < 0 || b > 49)
            {
                cout << "The worm ran off the board on move " << i + 1 << "." <<endl;
                break;
            }
            else
            {
                if(s2[a][b])
                {
                  continue;
                }

                else
                {
                 cout << "The worm ran into itself on move " << i + 1 <<"."<<endl;
                 break;
                }
            }
    }

}

return 0;

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-12-01 14:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图