POJ 2632 Crashing Robots

模拟题

不算特别麻烦,模拟两只小机器人的行为,判断他们是否会撞墙以及相撞

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct maze
{
    int x;
    int y;
    int dir;
};
int cdir(char t)
{
    if (t == 'E')
        return 1;
    else if (t == 'S')
        return 2;
    else if (t == 'W')
        return 3;
    return 4;
}
int main()
{
    int m[200][200];
    maze rob[200];
    int T,a,b,n,M,t1,t2,t3,eventrob,i,j,fail;
    char tc;
    scanf("%d",&T);
    while (T--)
    {

        scanf("%d%d",&a,&b);
        scanf("%d%d",&n,&M);
        memset(m,0,sizeof(m));

        for (i=1; i<=n; i++)
        {
            scanf("%d%d%*c%c",&t1,&t2,&tc);

            m[t2][t1]=i;
            rob[i].x=t2;
            rob[i].y=t1;
            rob[i].dir=cdir(tc);
            //printf("dir=%d\n",rob[i].dir);
        }
        fail=0;

        for (i=0; i<M; i++)
        {
            scanf("%d%*c%c%d",&t1,&tc,&t2);
            if (fail != 0)
                continue;
            //printf("->%d %d %d\n",rob[t1].x,rob[t1].y,rob[t1].dir);
            if (tc == 'F')
            {
                switch (rob[t1].dir)
                {
                    case 1:
                        m[rob[t1].x][rob[t1].y]=0;
                        for (j=0; j<t2 && fail == 0; j++)
                        {

                            rob[t1].y++;
                          //  printf("%d %d %d %d\n",rob[t1].x,rob[t1].y,m[rob[t1].x][rob[t1].y],a);
                            if (rob[t1].y > a)
                                fail=-1;
                            if (m[rob[t1].x][rob[t1].y] != 0)
                                fail=m[rob[t1].x][rob[t1].y];
                        }
                        m[rob[t1].x][rob[t1].y]=t1;
                    break;
                    case 2:
                        m[rob[t1].x][rob[t1].y]=0;
                        for (j=0; j<t2 && fail == 0; j++)
                        {
                             rob[t1].x--;
                            //printf("%d %d %d %d\n",rob[t1].x,rob[t1].y,m[rob[t1].x][rob[t1].y],a);

                            if (rob[t1].x == 0)
                                fail=-1;
                            if (m[rob[t1].x][rob[t1].y] != 0)
                                fail=m[rob[t1].x][rob[t1].y];
                        }
                        m[rob[t1].x][rob[t1].y]=t1;
                    break;
                    case 3:
                        m[rob[t1].x][rob[t1].y]=0;
                        for (j=0; j<t2 && fail == 0; j++)
                        {
                            rob[t1].y--;
                            //printf("%d %d %d %d\n",rob[t1].x,rob[t1].y,m[rob[t1].x][rob[t1].y],a);

                            if (rob[t1].y == 0)
                                fail=-1;
                            if (m[rob[t1].x][rob[t1].y] != 0)
                                fail=m[rob[t1].x][rob[t1].y];
                        }
                        m[rob[t1].x][rob[t1].y]=t1;
                    break;
                    case 4:
                        m[rob[t1].x][rob[t1].y]=0;
                        for (j=0; j<t2 && fail == 0; j++)
                        {

                           rob[t1].x++;
                           // printf("%d %d %d %d\n",rob[t1].x,rob[t1].y,m[rob[t1].x][rob[t1].y],a);
                            if (rob[t1].x > b)
                                fail=-1;
                            if (m[rob[t1].x][rob[t1].y] != 0)
                                fail=m[rob[t1].x][rob[t1].y];
                        }
                        m[rob[t1].x][rob[t1].y]=t1;
                    break;
                }
                if (fail != 0)
                    eventrob=t1;
            }
            else if (tc == 'L')
            {
                t2%=4;
                for (j=0; j<t2; j++)
                {
                    rob[t1].dir--;
                    if (rob[t1].dir == 0)
                        rob[t1].dir+=4;
                }

            }
            else if (tc == 'R')
            {
                t2%=4;
                for (j=0; j<t2; j++)
                {
                    rob[t1].dir++;
                    if (rob[t1].dir == 5)
                        rob[t1].dir-=4;
                }

            }
            //printf("!!%d\n",fail);
        }

        if (fail == 0)
        {
            printf("OK\n");
        }
        else if (fail == -1)
        {
            printf("Robot %d crashes into the wall\n",eventrob);
        }
        else
        {
            printf("Robot %d crashes into robot %d\n",eventrob,fail);
        }
    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值