POJ 2632 Crashing Robots

POJ 3480 Crashing Robots

[★★☆☆☆]模拟

  • 题目大意:

    输入仓库大小,和机器人的位置,朝向。根据指令进行操作,如果两个机器人相撞或者机器人撞墙则输出,结束。

  • 样例

    输入:
    4
    5 4
    2 2
    1 1 E
    5 4 W
    1 F 7
    2 F 7
    5 4
    2 4
    1 1 E
    5 4 W
    1 F 3
    2 F 1
    1 L 1
    1 F 3
    5 4
    2 2
    1 1 E
    5 4 W
    1 L 96
    1 F 2
    5 4
    2 3
    1 1 E
    5 4 W
    1 F 4
    1 L 1
    1 F 20

    输出:
    Robot 1 crashes into the wall
    Robot 1 crashes into robot 2
    OK
    Robot 1 crashes into robot 2

  • 解题思路:

    模拟水题。。没有算法,细心就行。

  • 代码
#include <iostream>

using namespace std;

const int MAX = 110;
int A, B;
int N, M;

struct RB {
    int x, y;
    char f;
};
struct ML {
    int r;
    char a;
    int c;
};

RB bot[MAX];
ML m[MAX];
int map[MAX][MAX];

void outmap() {
    for (int i = 1; i <= A; i++) {
        for (int j = 1; j <= B; j++) {
            cout << map[i][j];
        }
        cout << endl;
    }
}

void solve() {
    for (int i = 1; i <= M; i++) {
        ML t = m[i];
        if (t.a == 'L') {
            t.c %= 4;
            while (t.c --) {
                if (bot[t.r].f == 'N') bot[t.r].f = 'W';
                else if (bot[t.r].f == 'W') bot[t.r].f = 'S';
                else if (bot[t.r].f == 'E') bot[t.r].f = 'N';
                else if (bot[t.r].f == 'S') bot[t.r].f = 'E';
            }
        }
        else if (t.a == 'R') {
            t.c %= 4;
            while (t.c --) {
                if (bot[t.r].f == 'N') bot[t.r].f = 'E';
                else if (bot[t.r].f == 'W') bot[t.r].f = 'N';
                else if (bot[t.r].f == 'E') bot[t.r].f = 'S';
                else if (bot[t.r].f == 'S') bot[t.r].f = 'W';
            }
        }
        else if (t.a == 'F') {
            if (bot[t.r].f == 'N') {

                    while (t.c--) {

                        bot[t.r].y++;
                        if (map[bot[t.r].x][bot[t.r].y]) {
                            cout << "Robot " <<  t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
                            return;
                        }
                        else {
                            map[bot[t.r].x][bot[t.r].y] = t.r;
                            map[bot[t.r].x][bot[t.r].y-1] = 0;
                        }
                        if (bot[t.r].y >= B+1) {
                            cout << "Robot " << t.r << " crashes into the wall" << endl;
                            return;
                        }
                    }

            }
            else if (bot[t.r].f == 'S') {

                    while (t.c--) {

                        bot[t.r].y--;
                        if (map[bot[t.r].x][bot[t.r].y]) {
                            cout << "Robot " <<  t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
                            return;
                        }
                        else {
                            map[bot[t.r].x][bot[t.r].y] = t.r;
                            map[bot[t.r].x][bot[t.r].y+1] = 0;
                        }
                        if (bot[t.r].y <= 0) {
                            cout << "Robot " << t.r << " crashes into the wall" << endl;
                            return;
                        }
                    }

            }
            else if (bot[t.r].f == 'W') {

                    while (t.c--) {

                        bot[t.r].x--;
                        if (map[bot[t.r].x][bot[t.r].y]) {
                            cout << "Robot " <<  t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
                            return;
                        }
                        else {
                            map[bot[t.r].x][bot[t.r].y] = t.r;
                            map[bot[t.r].x+1][bot[t.r].y] = 0;
                        }
                        if (bot[t.r].x <= 0) {
                            cout << "Robot " << t.r << " crashes into the wall" << endl;
                            return;
                        }
                    }

            }
            else if (bot[t.r].f == 'E') {

                    while (t.c--) {

                        bot[t.r].x++;
                        if (map[bot[t.r].x][bot[t.r].y]) {
                            cout << "Robot " <<  t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
                            return;
                        }
                        else {
                            map[bot[t.r].x][bot[t.r].y] = t.r;
                            map[bot[t.r].x-1][bot[t.r].y] = 0;
                        }
                        if (bot[t.r].x >= A+1) {
                            cout << "Robot " << t.r << " crashes into the wall" << endl;
                            return;
                        }
                    }
            }
        }


    }
    cout << "OK" << endl;
}
int main() {
    int TT;
    cin >> TT;
    while (TT--) {
        for (int i = 1; i <= 100; i++) {
            for (int j = 1; j <= 100; j++) {
                map[i][j] = 0;
            }
        }
        cin >> A >> B;
        cin >> N >> M;
        for (int i = 1; i <= N; i++) {
            cin >> bot[i].x >> bot[i].y >> bot[i].f;
            map[bot[i].x][bot[i].y] = i;
        }
        for (int i = 1; i <= M; i++) {
            cin >> m[i].r >> m[i].a >> m[i].c;
        }
        solve();




    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值