zoj 1056 一遍过

代码里是用二维数组来模拟蠕虫身体,有的博主是用结构体,当然结构体更加符合这种工作。

a[i][0]是纵坐标y,a[i][1]是横坐标x

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.

#include<stdio.h>
int a[20][2];   //二维数组 a[0]是蠕虫尾部 a[19]是蠕虫头部
int retrieve(void)  //检索蠕虫
{
    if(a[19][0]>50||a[19][1]>50||a[19][0]<1||a[19][1]<1)    //判断蠕虫头部是否超出棋盘格
        return 2;
    int y=a[19][0],x=a[19][1];
    for(int i=0;i<19;i++)                                   //检查蠕虫的头部的坐标值是否与身体某处相等
        if(a[i][0]==y&&a[i][1]==x)
            return 1;
    return 0;
}
void rexy(void)
{
    for(int i=0;i<19;i++)         //蠕虫移动后除头部以外的身体重新赋值,移动了当然坐标值都变了,所以要重新赋值,头部下面会赋值的
        a[i][0]=a[i+1][0],a[i][1]=a[i+1][1];
}
int main()
{
    int n,flag,i;
    char s[100];
    while(scanf("%d",&n),n)
    {
        scanf("%s",s);
        for(int i=0;i<20;i++)           //打表,每一次测试都要重新打一次表,因为数组内的值是每次测试移动过后的坐标值
            a[i][0]=25,a[i][1]=i+11;
        for(i=0;i<n;i++)
        {
            rexy();         //移动
            switch(s[i])        //匹配E S W N
            {
                case 'E':
                    a[19][1]++;  //向东 x++
                    break;
                case 'S':
                    a[19][0]++; //向南 y++
                    break;
                case 'W':
                    a[19][1]--; //向西 x--
                    break;
                case 'N':
                    a[19][0]--; //向北 y--
                    break;
            }
            flag=retrieve();
            if(flag==1)                 //retrieve返回值1是出界,2是撞到身体,i+1则是记录了到哪一步了
            {
                printf("The worm ran into itself on move %d.\n",i+1);
                break;
            }
            else if(flag==2)
            {
                printf("The worm ran off the board on move %d.\n",i+1);
                break;
            }
        }
        if(!flag)           //返回值为0则说明正确执行步骤
            printf("The worm successfully made all %d moves.\n",n);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Heredy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值