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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝