现在的位置: 首页 > 算法 > 正文

poj 2632 Crashing Robots, 模拟

2018年12月19日 算法 ⁄ 共 1361字 ⁄ 字号 评论关闭

点击打开链接

简单

模拟机器人的移动,发生碰撞时输出相应的信息。

code

#include <cstdio>
#include <cstring>
using namespace std;
struct node{
    int k;
    int s;
}
mtx[200][200];

struct node1{
    int x, y;
}
rob[200];

int n, m;
int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; //N, E, S, W

int D(char ch){
    if(ch=='N') return 0;
    if(ch=='E') return 1;
    if(ch=='S') return 2;
    if(ch=='W') return 3;
}

int A(int x, char ch){
    if(ch=='L') return (x+3) % 4;
    if(ch=='R') return (x+1) % 4;
    if(ch=='F') return  x;
}

int main()
{
    int Robots, Actions, t, i, j;
    int x, y;
    char ch;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d",&n, &m);
        scanf("%d%d", &Robots, &Actions);
        for(i=0; i<=n; ++i) for(j=0; j<=m; ++j) mtx[i][j].k = -1;
        for(i=1; i<=Robots; ++i){
            scanf("%d %d %c", &x, &y, &ch);
            mtx[x][y].k = i;
            mtx[x][y].s = D(ch);
            rob[i].x = x;
            rob[i].y = y;
        }
        int flag = 0, rob1, rob2;
        int wh, len;
        for(i=1; i<=Actions; ++i){
            scanf("%d %c %d", &wh, &ch, &len);
            if(flag) continue;
            int nowx=rob[wh].x, nowy=rob[wh].y;
            int dr =D(mtx[nowx][nowy].s);
            if(ch!='F'){
                for(j=0; j<len%4; ++j)
                    dr = A(dr, ch);
                mtx[nowx][nowy].s = dr;
                continue;
            }

            for(j=0; j<len; ++j){
                nowx += dir[dr][0];
                nowy += dir[dr][1];
                if(nowx==0 || nowx==n+1 || nowy==0 || nowy==m+1)
                {
                    flag= 1;
                    rob1 = wh;
                    break;
                }
                if(mtx[nowx][nowy].k != -1)
                {
                    flag = 2;
                    rob1 = wh;
                    rob2 = mtx[nowx][nowy].k;
                    break;
                }
            }
            if(j==len){
                mtx[rob[wh].x][rob[wh].y].k = -1;
                rob[wh].x = nowx;
                rob[wh].y = nowy;
                mtx[nowx][nowy].k = wh;
                mtx[nowx][nowy].s = dr;
            }
        }
        if(flag==0) printf("OK\n");
        else if(flag==1) printf("Robot %d crashes into the wall\n", rob1);
        else if(flag==2) printf("Robot %d crashes into robot %d\n", rob1, rob2);
    }
    return 0;
}

抱歉!评论已关闭.