POJ 2632 Crashing Robots(简单模拟)

19 篇文章 0 订阅

Description
在一张a*b的地图上有n个机器人,先给出这n个机器人的位置和朝向(具体方向如下图,为用例一的机器人初始位置及朝向),然后对这n个机器人进行m次操作,每次操作是对某个机器人进行某种操作多次,操作分三种:
L:机器人左转90度
R:机器人右转90度
F:机器人前进一格
Input
第一行为一整数T表示用例组数,每组用例第一行为两个整数a和b表示地图的1行列数,第二行两个整数n和m表示机器人个数和操作数,之后n行每行两个整数x,y表示该机器人位置,一个字符表示机器人朝向,最后m行每行以< robot #> < action> < repeat>的形式输入
Output
如果操作过程中机器人i撞到了机器人j则输出Robot i crashes into robot j,如果机器人i碰到墙则输出Robot i crashes into the wall,如果上述情况均未发生则输出OK
Sample Input
4
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20
Sample Output
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2
Solution
简单题,按题意模拟机器人运动过程即可
Code

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 111
struct node
{
    int x,y,to;
}r[maxn];
int T,a,b,n,m,mark[maxn][maxn];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int Action(int robot,char *action,int repeat)
{
    if(action[0]=='F')
    {
        while(repeat--)
        {
            mark[r[robot].x][r[robot].y]=0;
            int x=r[robot].x+dx[r[robot].to],y=r[robot].y+dy[r[robot].to];
            if(x<1||x>a||y<1||y>b)
            {
                printf("Robot %d crashes into the wall\n",robot);
                return 0;
            }
            if(mark[x][y])
            {
                printf("Robot %d crashes into robot %d\n",robot,mark[x][y]);
                return 0;
            }
            r[robot].x=x,r[robot].y=y;
            mark[x][y]=robot;
        }
    }
    else 
    {
        if(action[0]=='L')r[robot].to=(r[robot].to+3*repeat)%4;
        else r[robot].to=(r[robot].to+repeat)%4;
    }
    return 1;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(mark,0,sizeof(mark));
        scanf("%d%d%d%d",&a,&b,&n,&m);
        for(int i=1;i<=n;i++)
        {
            char dir[3];
            scanf("%d%d%s",&r[i].x,&r[i].y,dir);
            if(dir[0]=='W')r[i].to=0;
            else if(dir[0]=='N')r[i].to=1;
            else if(dir[0]=='E')r[i].to=2;
            else r[i].to=3;
            mark[r[i].x][r[i].y]=i;
        }
        int flag=1;
        while(m--)
        {
            int robot,repeat;char action[3];
            scanf("%d%s%d",&robot,action,&repeat);
            if(flag)flag=Action(robot,action,repeat);
        }
        if(flag)printf("OK\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值