zoukankan      html  css  js  c++  java
  • POJ2632 Crashing Robots(模拟)

    题目链接

    分析:

    虽说是简单的模拟,却调试了很长时间。

    调试这么长时间总结来的经验:

    1.坐标系要和题目建的一样,要不就会有各种麻烦。

    2.在向前移动过程中碰到其他的机器人也不行,这个题目说啦:a robot always completes its move before the next one starts moving。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int maxn = 100 + 10;
    
    int G[maxn][maxn];
    
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    
    struct Robots{
        int x, y, d;
    }r[100+10];
    
    int main(){
        int A, B, n, m, T, flag, rob, cra_rob;
        char s[5];
    
        scanf("%d", &T);
    
        while(T--) {
            flag = 0;
    
            memset(G, 0, sizeof(G));
    
            scanf("%d %d", &A, &B);
            scanf("%d %d", &n, &m);
    
            for(int i=1; i<=n; i++) {
                scanf("%d %d", &r[i].x, &r[i].y);
                scanf("%s", s);
                G[r[i].y][r[i].x] = i;
    
                switch(s[0]) {
                case 'N': r[i].d = 0; break;
                case 'E': r[i].d = 1; break;
                case 'S': r[i].d = 2; break;
                case 'W': r[i].d = 3; break;
                }
            }
    
            int num, rep;
            char act[3];
    
            while(m--) {
                scanf("%d %s %d", &num, act, &rep);
                if(!flag) {
                    if(act[0] == 'L') r[num].d = ((r[num].d - rep)%4+4)%4;
                    else if(act[0] == 'R') r[num].d = (r[num].d + rep) % 4;
                    else {
                        G[r[num].y][r[num].x] = 0;
                        for(int i=0; i<rep; i++) {
                            r[num].x += dx[r[num].d];
                            r[num].y += dy[r[num].d];
    
                            if(r[num].x <= 0 || r[num].y <= 0 || r[num].x > A || r[num].y > B) {
                                 rob = num;
                                 flag = 1;
                                 break;
                            } else if(G[r[num].y][r[num].x]) {
                                rob = num;
                                cra_rob = G[r[num].y][r[num].x];
                                flag = 2;
                                break;
                            }
                        }
    
                        if(!flag) G[r[num].y][r[num].x] = num;
                    }
                }
            }
    
            if(flag == 1) printf("Robot %d crashes into the wall
    ", rob);
            else if(flag == 2) printf("Robot %d crashes into robot %d
    ", rob, cra_rob);
            else printf("OK
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    HDOJ 2095 find your present (2)
    HDOJ 2186 悼念512汶川大地震遇难同胞——一定要记住我爱你
    九度 1337 寻找最长合法括号序列
    九度 1357 疯狂地Jobdu序列
    HDOJ 1280 前m大的数
    九度 1343 城际公路网
    九度 1347 孤岛连通工程
    HDOJ 2151 Worm
    九度 1342 寻找最长合法括号序列II
    九度 1346 会员积分排序
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3148985.html
Copyright © 2011-2022 走看看