poj 2632 Crashing Robots

本文解析了POJ 2632 Crashing Robots题目,通过模拟机器人在地图上的移动来判断是否会碰撞到墙壁或其他机器人。使用C++实现,涉及机器人方向调整、位置更新及碰撞检测等关键步骤。

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

恶心的模拟题,敲错了字符,错了……

/*
 * Author: stormdpzh
 * POJ: 2632 Crashing Robots
 * Time: 2012/5/12 12:02:49
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>

#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define wh(n) while(scanf("%d", &n) != EOF)
#define whz(n) while(scanf("%d", &n) != EOF && n != 0)
#define lint long long

using namespace std;

const int MaxR = 105;

struct Robot {
    int x, y;
    int dir;
} robot[MaxR];
int n, m;
int a, b;
int kind, rno;
bool flag;

void getPlace(int *xx, int *yy, int id)
{
    if(robot[id].dir == 0) {
        *xx = robot[id].x + 1;
        *yy = robot[id].y;
    }
    else if(robot[id].dir == 1) {
        *xx = robot[id].x;
        *yy = robot[id].y + 1;
    }
    else if(robot[id].dir == 2) {
        *xx = robot[id].x - 1;
        *yy = robot[id].y;
    }
    else if(robot[id].dir == 3) {
        *xx = robot[id].x;
        *yy = robot[id].y - 1;
    }
}

int check(int xx, int yy)
{
    repf(i, 1, n) {
        if(robot[i].x == xx && robot[i].y == yy)
            return i;
    }
    return 0;
}

void solve()
{
    int no, times;
    char opt;
    int tmpx, tmpy;
    rep(i, m) {
        cin >> no >> opt >> times;
        if(!flag) rep(j, times) {
            switch(opt) {
                case 'L': 
                    robot[no].dir = (robot[no].dir + 1) % 4; 
                    break;
                case 'R': 
                    robot[no].dir = (4 + robot[no].dir - 1) % 4; 
                    break;
                case 'F': 
                    getPlace(&tmpx, &tmpy, no);
                    if(tmpx < 1 || tmpx > a || tmpy < 1 || tmpy > b) {
                        kind = 0;
                        rno = no;
                        flag = true;
                    }
                    else {
                        int res = check(tmpx, tmpy);
                        if(res > 0) {
                            flag = true;
                            rno = no;
                            kind = res;
                        }
                        else {
                            robot[no].x = tmpx;
                            robot[no].y = tmpy;
                        }    
                    }
                    break;
            }
            if(flag) break;
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &a, &b);
        scanf("%d%d", &n, &m);
        repf(i, 1, n) {
            char d;
            cin >> robot[i].x >> robot[i].y >> d;
            if(d == 'E') robot[i].dir = 0;
            else if(d == 'N') robot[i].dir = 1;
            else if(d == 'W') robot[i].dir = 2;
            else if(d == 'S') robot[i].dir = 3;
        }
        flag = false;
        kind = -1;
        solve();
        if(kind == -1) 
            printf("OK\n");
        else if(kind == 0)
            printf("Robot %d crashes into the wall\n", rno);
        else
            printf("Robot %d crashes into robot %d\n", rno, kind);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值