poj 2632 Crashing Robots

本文介绍了一种通过直接模拟来解决机器人路径规划问题的方法,包括处理机器人在限定区域内的移动指令、转向及前进操作,并判断是否会发生碰撞或触壁的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:在一个 AXB 的平面有 N 个机器人,给出 N 个机器人的起始目标和移动方向,然后给出 M 条指令,指令格式为:< robot #> < action> < repeat>,依次为机器人编号、指令内容、执行次数。

指令内容:L 表示左转90度;R 表示右转90度;F 表示向前移动一步。

指令按顺序执行,执行完所有指令后,若过程中有机器人发生碰撞,输出第一次碰撞;若是没有发生碰撞,输出”OK“。

Output:
•Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)
•Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.
•OK, if no crashing occurs.

分析:直接模拟…..

#include<cstdio>
#include<cstring>
using namespace std;
const int MAX = 101;
const int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

struct Robot {
    int d, x, y;
}r[MAX];
int mp[MAX][MAX], A, B;

int main() {
    char dir, op;
    int t, n, m, id, cnt, crash;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d%d", &B, &A, &n, &m);
        memset(mp, 0, sizeof(mp));
        for(int i = 1; i <= n; i++) {
            scanf("%d%d %c", &r[i].y, &r[i].x, &dir);
            if(dir == 'W') { r[i].d = 0; }
            else if(dir == 'N') { r[i].d = 1; }
            else if(dir == 'E') { r[i].d = 2; }
            else { r[i].d = 3; }
            mp[r[i].x][r[i].y] = i;
        }
        crash = 0;
        for(int i = 0; i < m; i++) {
            scanf("%d %c%d", &id, &op, &cnt);
            if(crash) { continue; }
            Robot &rb = r[id];
            if(op != 'F') { cnt %= 4; }
            for(int j = 0; j < cnt; j++) {
                if(op == 'L') {
                    rb.d--;
                    if(rb.d < 0) {
                        rb.d += 4;
                    }
                }
                else if(op == 'R') {
                    rb.d++;
                    if(rb.d > 3) {
                        rb.d -= 4;
                    }
                }
                else {
                    int nx = rb.x + dx[rb.d], ny = rb.y + dy[rb.d];
                    if(mp[nx][ny]) {
                        printf("Robot %d crashes into robot %d\n", id, mp[nx][ny]);
                        crash = 1;
                        break;
                    }
                    else if(nx > A || ny > B || !nx || !ny) {
                        printf("Robot %d crashes into the wall\n", id);
                        crash = 1;
                        break;
                    }
                    else {
                        mp[rb.x][rb.y] = 0;
                        mp[rb.x = nx][rb.y = ny] = id;
                    }
                }
            }
        }
        if(!crash) { puts("OK"); }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值