zoukankan      html  css  js  c++  java
  • 【POJ2632】Crashing Robots

    题目传送门

    本题知识点:模拟

    模拟机器人的运作过程,分别有三种功能,L 则是左转90°,R 则是右转90°,L 则是前进1格。让我们去模拟并判断它们的状态。

    输入:

    第一行是测试样例

    第二行分别是矩形的长、宽(比如,5 4)

    // 题目是讲得很清楚的
    // 4
    // 3
    // 2
    // 1
    //   1 2 3 4 5 
    

    第三行分别是机器人的个数以及指令条数

    接下来是先输入每个机器人的初始状态(有多少个机器人就多少行),3个数值分别对应机器人所在位置的(x, y)以及机器人所面向的方向(因为 F 指令只能让机器人向前,所以懂得东南西北是很重要的一件事)

    同样,接下来有多少条指令就输入多少行,3个数值分别对应机器人的号数,对应指令,以及执行该指令的次数。

    等一切输入完后就可以执行(模拟)了。

    输出有三种情况

    一种是撞墙的

    一种是撞机器人的

    剩下的是每条指令都通过并没有发生意外(撞墙或撞机器人)的

    对于前两种,我们得只输出一次就可以不管后面的了,因为题目要求只输出一次情况

    但如果在输入时就开始模拟的话,中断了就会影响后面的输入,所以我弄了个结构体去存执行指令

    剩下的,就看自己是否细心与耐心了

    我语言比较菜,所以代码写得很长,很暴力,感觉有些地方还可以写的简练一点,但由于自己懒,想快水过去好睡觉了。

    数据很小。

    // POJ 2632
    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    int T;
    int W, H, robot_n, t;
    struct node{
        int w, h;
        char to;
    }rob[102];
    struct e{
        int who, re;
        char done;
    }take[102];
    //char pla[102][102];
    
    void show(){
        for(int i = 1; i <= robot_n; i++){
            printf("i:%d w:%d h:%d
    ", i, rob[i].w, rob[i].h);
        }
    }
    
    int main()
    {
        scanf("%d", &T);
        while(T--){
            bool last = true;
            // take in
            scanf("%d %d", &W, &H);
            scanf("%d %d", &robot_n, &t);
            for(int i = 1; i <= robot_n; i++) scanf("%d %d %c", &rob[i].w, &rob[i].h, &rob[i].to);
            for(int i = 1; i <= t; i++) scanf("%d %c %d", &take[i].who, &take[i].done, &take[i].re);
    
            // done!
            for(int i= 1; i <= t; i++){
                int it = take[i].who;
                char bi = take[i].done;
                int num = take[i].re;
    
                // 转向
                if(bi == 'L'){
                    num = num % 4;
                    while(num > 0){
                        if(rob[it].to == 'N') rob[it].to = 'W';
                        else if(rob[it].to == 'W') rob[it].to = 'S';
                        else if(rob[it].to == 'S') rob[it].to = 'E';
                        else if(rob[it].to == 'E') rob[it].to = 'N';
                        num--;
                    }
                }
                else if(bi == 'R'){
                    num = num % 4;
                    while(num > 0){
                        if(rob[it].to == 'N') rob[it].to = 'E';
                        else if(rob[it].to == 'E') rob[it].to = 'S';
                        else if(rob[it].to == 'S') rob[it].to = 'W';
                        else if(rob[it].to == 'W') rob[it].to = 'N';
                        num--;
                    }
                }
                // 前进
                else if(bi == 'F'){
                    char go = rob[it].to;
                    // 北
                    if(go == 'N'){
                        while(num > 0){
                            rob[it].h++;
                            // wall
                            if(rob[it].h > H){
                                printf("Robot %d crashes into the wall
    ", it);
                                last = false;
                                break;
                            }
                            // robot
                            bool can = true; // 跳出遍历
                            for(int i = 1; i <= robot_n; i++){
                                if(i == it) continue;
                                if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                    printf("Robot %d crashes into robot %d
    ", it, i);
                                    can = false;
                                    last = false;
                                    break;
                                }
                            }
                            if(!can) break;
                            num--;
                        }
                    }
                    // 西
                    else if(go == 'W'){
                        while(num > 0){
                            rob[it].w--;
                            // wall
                            if(rob[it].w < 1){
                                printf("Robot %d crashes into the wall
    ", it);
                                last = false;
                                break;
                            }
                            // robot
                            bool can = true;
                            for(int i = 1; i <= robot_n; i++){
                                if(i == it) continue;
                                if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                    printf("Robot %d crashes into robot %d
    ", it, i);
                                    can = false;
                                    last = false;
                                    break;
                                }
                            }
                            if(!can) break;
                            num--;
                        }
                    }
                    // 南
                    else if(go == 'S'){
                        while(num > 0){
                            rob[it].h--;
                            // wall
                            if(rob[it].h < 1){
                                printf("Robot %d crashes into the wall
    ", it);
                                last = false;
                                break;
                            }
                            // robot
                            bool can = true;
                            for(int i = 1; i <= robot_n; i++){
                                if(i == it) continue;
                                if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                    printf("Robot %d crashes into robot %d
    ", it, i);
                                    can = false;
                                    last = false;
                                    break;
                                }
                            }
                            if(!can) break;
                            num--;
                        }
                    }
                    // 东
                    else if(go == 'E'){
                        while(num > 0){
                            rob[it].w++;
                            // wall
                            if(rob[it].w > W){
                                printf("Robot %d crashes into the wall
    ", it);
                                last = false;
                                break;
                            }
                            // robot
                            bool can = true;
                            for(int i = 1; i <= robot_n; i++){
                                if(i == it) continue;
                                if(rob[it].h == rob[i].h && rob[it].w == rob[i].w){
                                    printf("Robot %d crashes into robot %d
    ", it, i);
                                    can = false;
                                    last = false;
                                    break;
                                }
                            }
                            if(!can) break;
                            num--;
                        }
                    }
                }
                // 中断剩余操作防止过多输出
                if(!last) break;
            }
            // 跳过 OK
            if(!last) continue;
            printf("OK
    ");
        }
        return 0;
    }
    
    
  • 相关阅读:
    大数据学习13_MapReduce计数器&排序和序列化
    大数据学习12_MapReduce分区
    大数据学习11_MapReduce案例实战(单词统计)
    大数据学习10_Mapreduce
    大数据学习09_HDFS3
    周总结8
    《河北省重大技术需求征集系统》10_导入大量数据做测试
    《河北省重大技术需求征集系统》08_需求浏览
    《河北省重大技术需求征集系统》07_综合查询
    《河北省重大技术需求征集系统》06_网络审核
  • 原文地址:https://www.cnblogs.com/Ayanowww/p/11531205.html
Copyright © 2011-2022 走看看