ZOJ - The Worm Turns(模拟)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzyws739307453/article/details/85142692

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1056


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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.

扫描二维码关注公众号,回复: 4659278 查看本文章

Problem solving:

简单的模拟题,模拟一下蠕虫走的过程就行了。我们先把蠕虫的身体都记录下来,并把它们标记下来。只需要在每次移动的过程中判断一下就行了。

#include <stdio.h>
#include <string.h>
struct edge {
    int x, y;
}e[25], t;
int main()
{
    char str[110];
    int n, i, temp, map[55][55];
    while (~scanf("%d", &n), n)
    {
        temp = 3;
        scanf("%s", str);
        memset(map, 0, sizeof(map));
        for (i = 0; i < 20; i++)
        {
            e[i].x = 25;
            e[i].y = 30 - i;
            map[e[i].x][e[i].y] = 1;
        }
        t = (edge){25, 30};
        for (i = 0; i < n; i++)
        {
            switch(str[i])
            {
                case 'E': t.y++; break;
                case 'W': t.y--; break;
                case 'N': t.x--; break;
                case 'S': t.x++; break;
            }
            map[e[19].x][e[19].y] = 0;
            for (int j = 19; j > 0; j--)
                e[j] = e[j - 1];
            if (t.x > 50 || t.x < 1 || t.y > 50 || t.y < 1)
            {
                i++;
                temp = 2;
                break;
            }
            if (map[t.x][t.y])
            {
                i++;
                temp = 1;
                break;
            }
            e[0].x = t.x;
            e[0].y = t.y;
            map[e[0].x][e[0].y] = 1;
        }
        if (temp == 1)
            printf("The worm ran into itself on move %d.\n", i);
        else if (temp == 2)
            printf("The worm ran off the board on move %d.\n", i);
        else printf("The worm successfully made all %d moves.\n", i);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzyws739307453/article/details/85142692
ZOJ