POJ 2632

本文介绍了一个基于C++实现的机器人路径模拟程序。该程序通过定义机器人类和指令类,模拟了多台机器人在一个限定区域内根据一系列指令移动的过程。文章详细解释了如何避免机器人之间的碰撞以及与边界的碰撞。

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

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <vector>
using namespace std;
const int MAXSIZE = 25;
int testcase;
int A, B; // 东西,南北方向
int N, M; // 机器人数量, 指令数量
int direction[4] = {0, 1, 2, 3}; //分别表示上左下右
class MInstruction{
public:
    int robotId;
    char action;
    int repeat;
    MInstruction(int robotid, char action, int repeat){
        this->robotId = robotid;
        this->action = action;
        this->repeat = repeat;
    }
};
class Robot{
public:
    int robotID;
    pair<int, int> pairs;
    int direction;
    Robot(int robotID, pair<int ,int >pairs, char direction):
            robotID(robotID), pairs(pairs),direction(direction){}
};
bool isin(pair<int, int > pair1){
    int h = pair1.first;
    int v = pair1.second;
    return h>=1&&h<=A&&v>=1&&v<=B;
}
vector<MInstruction> insvec;
vector<Robot> robotvec;
bool exeins(Robot & robot, MInstruction & instuction){
    // 首先检查指令
    char d = instuction.action;
    if (d == 'F'){
        // 直走,每走一步,检查是否撞墙或者撞到机器人
        for (int j = 0; j < instuction.repeat; ++j) {
            if (robot.direction == 0) robot.pairs.second++;
            else if (robot.direction == 1) robot.pairs.first--;
            else if (robot.direction == 2) robot.pairs.second--;
            else if (robot.direction == 3) robot.pairs.first++;
            for (int i = 0; i <N; ++i) {
                if (!isin(robot.pairs)){
                    cout<<"Robot "<<robot.robotID<<" crashes into the wall"<<endl;
                    return false;
                }
                if (robotvec[i].robotID == robot.robotID) continue;
                if (robot.pairs.first == robotvec[i].pairs.first&&
                        robot.pairs.second == robotvec[i].pairs.second){
                    cout<<"Robot "<<robot.robotID<<" crashes into robot "<<robotvec[i].robotID<<endl;
                    return false;
                }
            }
        }
    } else if (d == 'L'){
        int r = instuction.repeat;
        robot.direction = (robot.direction+r)%4;
        // 左边转
    } else if (d == 'R'){
        int r = instuction.repeat%4;
        robot.direction = (robot.direction-r+4)%4;
        // 右边转
    }
    return true;
}
int getdir(char d){
    switch (d){
        case 'N':
            return 0;
        case 'W':
            return 1;
        case 'S':
            return 2;
        case 'E':
            return 3;
        default:
            return -1;
    }
}
int main(){
//    freopen("../in.txt", "r", stdin);
    cin>>testcase;
    while (testcase--){
        insvec.clear();
        robotvec.clear();
        cin>>A>>B>>N>>M;
        for (int i = 1; i <= N ; ++i) {
            int x, y;
            char dir;
            cin>>x>>y>>dir;
            int temp = getdir(dir);
            robotvec.push_back(Robot(i, make_pair(x, y),temp));
        }
        for (int i = 0; i < M; ++i) {
            int robotid;
            char dir;
            int times;
            cin>>robotid>>dir>>times;
            insvec.push_back(MInstruction(robotid, dir, times));
        }
        // 开始模拟
        for (int i = 0; i < M; ++i) {
            MInstruction ins = insvec[i];
            for (int j = 0; j <N ; ++j) {
                if (ins.robotId == robotvec[j].robotID){
                    // 说明找到了
                    if (exeins(robotvec[j], ins)){
                        break;
                    } else{
                        // 说明没有执行成功,结束这一轮
                        goto die;
                    }
                }
            }
        }
        cout<<"OK"<<endl;
        continue;
        die:
        continue;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值