poj-2632-Crashing Robots

本文介绍了一种通过模拟机器人在限定区域内的运动路径来检测碰撞的方法。利用C++实现了一个程序,该程序能够根据输入的机器人初始位置、方向及一系列指令,判断机器人是否会撞墙或相互碰撞,并给出相应的输出。

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

传送门

大意:一个仓库里面有几个机器人,给你机器人的初始位置及方向,然后让机器人执行一些指令,输出指令执行结果
如:
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.

直接模拟机器人就行了,

tip:用map把方向转换成数字,这样每次转方向的时候只要取模就行了

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <string> 
#include <map>
#include <algorithm>
#define N 110
#define ll long long
using namespace std;
struct robot{
    int x,y;
    int dir;
}r[N];
int Map[N][N], a, b, n, m;
map<char, int> mm;
bool fun(int num, int rep){
    int i, x = r[num].x, y = r[num].y;
    if (r[num].dir == 0){
        for (i = 1; i <= rep; i++){
            if (x+i > a){
                printf("Robot %d crashes into the wall\n", num);
                return true;
            }
            if (Map[x+i][y]){
                printf("Robot %d crashes into robot %d\n", num, Map[x+i][y]);
                return true;
            }else{
                Map[x+i-1][y] = 0;
                Map[x+i][y] = num;
                r[num].x = x+i;
            }
        }
    }else if (r[num].dir == 1){
        for (i = 1; i <= rep; i++){
            if (y-i < 1){
                printf("Robot %d crashes into the wall\n", num);
                return true;
            }
            if (Map[x][y-i]){
                printf("Robot %d crashes into robot %d\n", num, Map[x][y-i]);
                return true;
            }else{
                Map[x][y-i+1] = 0;
                Map[x][y-i] = num;
                r[num].y = y-i;
            }
        }
    }else if (r[num].dir == 2){
        for (i = 1; i <= rep; i++){
            if (x-i < 1){
                printf("Robot %d crashes into the wall\n", num);
                return true;
            }
            if (Map[x-i][y]){
                printf("Robot %d crashes into robot %d\n", num, Map[x-i][y]);
                return true;
            }else{
                Map[x-i+1][y] = 0;
                Map[x-i][y] = num;
                r[num].x = x-i;
            }
        }
    }else{
        for (i = 1; i <= rep; i++){
            if (y+i > b){
                printf("Robot %d crashes into the wall\n", num);
                return true;
            }
            if (Map[x][y+i]){
                printf("Robot %d crashes into robot %d\n", num, Map[x][y+i]);
                return true;
            }else{
                Map[x][y+i-1] = 0;
                Map[x][y+i] = num;
                r[num].y = y+i;
            }
        }
    }

    return false;
}
int main(){
#ifndef ONLINE_JUDGE 
    freopen("1.txt", "r", stdin);
#endif
    int i, j, T;
    int num, rep;
    char act;
    bool flag;
    mm.insert(pair<char, int>('E', 0));
    mm.insert(pair<char, int>('S', 1));
    mm.insert(pair<char, int>('W', 2));
    mm.insert(pair<char, int>('N', 3));
    scanf("%d", &T) ;
    while(T--){
        flag = false;
        memset(Map, 0, sizeof(Map));
        scanf("%d%d", &a, &b);
        scanf("%d%d", &n, &m);
        for (i = 1; i <= n; i++){
            cin >> r[i].x >> r[i].y >> act;
            r[i].dir = mm[act];
            Map[r[i].x][r[i].y] = i;
        }
        for (i = 0; i < m; i++){
            cin >> num >> act >> rep;
            if (flag)   continue;
            switch(act){
                case 'L': r[num].dir = ((r[num].dir-rep)%4+4)%4; break;
                case 'R': r[num].dir = (r[num].dir+rep)%4; break;
                case 'F': flag = fun(num, rep); break;
            }
        }
        if (!flag){
            printf("OK\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值