poj 2632 Crashing Robots, 模拟

点击打开链接


简单

模拟机器人的移动,发生碰撞时输出相应的信息。


code

#include <cstdio>
#include <cstring>
using namespace std;
struct node{
    int k;
    int s;
}
mtx[200][200];

struct node1{
    int x, y;
}
rob[200];

int n, m;
int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; //N, E, S, W

int D(char ch){
    if(ch=='N') return 0;
    if(ch=='E') return 1;
    if(ch=='S') return 2;
    if(ch=='W') return 3;
}

int A(int x, char ch){
    if(ch=='L') return (x+3) % 4;
    if(ch=='R') return (x+1) % 4;
    if(ch=='F') return  x;
}

int main()
{
    int Robots, Actions, t, i, j;
    int x, y;
    char ch;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d",&n, &m);
        scanf("%d%d", &Robots, &Actions);
        for(i=0; i<=n; ++i) for(j=0; j<=m; ++j) mtx[i][j].k = -1;
        for(i=1; i<=Robots; ++i){
            scanf("%d %d %c", &x, &y, &ch);
            mtx[x][y].k = i;
            mtx[x][y].s = D(ch);
            rob[i].x = x;
            rob[i].y = y;
        }
        int flag = 0, rob1, rob2;
        int wh, len;
        for(i=1; i<=Actions; ++i){
            scanf("%d %c %d", &wh, &ch, &len);
            if(flag) continue;
            int nowx=rob[wh].x, nowy=rob[wh].y;
            int dr =D(mtx[nowx][nowy].s);
            if(ch!='F'){
                for(j=0; j<len%4; ++j)
                    dr = A(dr, ch);
                mtx[nowx][nowy].s = dr;
                continue;
            }

            for(j=0; j<len; ++j){
                nowx += dir[dr][0];
                nowy += dir[dr][1];
                if(nowx==0 || nowx==n+1 || nowy==0 || nowy==m+1)
                {
                    flag= 1;
                    rob1 = wh;
                    break;
                }
                if(mtx[nowx][nowy].k != -1)
                {
                    flag = 2;
                    rob1 = wh;
                    rob2 = mtx[nowx][nowy].k;
                    break;
                }
            }
            if(j==len){
                mtx[rob[wh].x][rob[wh].y].k = -1;
                rob[wh].x = nowx;
                rob[wh].y = nowy;
                mtx[nowx][nowy].k = wh;
                mtx[nowx][nowy].s = dr;
            }
        }
        if(flag==0) printf("OK\n");
        else if(flag==1) printf("Robot %d crashes into the wall\n", rob1);
        else if(flag==2) printf("Robot %d crashes into robot %d\n", rob1, rob2);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值