POJ2632

来源:互联网 发布:网络经典流行语句 编辑:程序博客网 时间:2024/05/21 16:21

Problem: Crashing Robots
Description: 机器人移动。给你一些机器人的初始位置和朝向和一些移动指令。要你判断在指令执行的过程中是否有机器人撞墙或者两个机器人相撞的情况。
Solution: 直接模拟就好了,不过要注意几个问题,题中位置和方向的给出与我们程序中的朝向是不一样的,因此需要转化;在判断两个机器人是否碰撞时要一步一步地判断,不能写成代码中注释地方的那样。
Code(C++):

#include <stdio.h>#include <string.h>typedef struct tagNode{    int x,y;    int dir;}Node;const int M=105;int A,B;int n,m;int map[M][M];Node node[M];int get_dir(char c){    switch(c){    case 'N':        return 1;    case 'E':        return 2;    case 'S':        return 3;    case 'W':        return 0;    }    return -1;}int main(){    int N;    for(scanf("%d",&N);N--;){        memset(map,0,sizeof(map));        scanf("%d%d%d%d",&A,&B,&n,&m);        int x,y;        char c[10];        for(int i=0;i<n;i++){            scanf("%d%d%s",&x,&y,c);            map[x][y]=i+1;            node[i+1].x=x;            node[i+1].y=y;            node[i+1].dir=get_dir(c[0]);        }        bool f=false;        int I=0,num=0;        for(int L=0;L<m;L++){            scanf("%d%s%d",&I,c,&num);            if(f)                continue;            Node tmp=node[I];            map[node[I].x][node[I].y]=0;            if(c[0]=='F'){                if(tmp.dir==0)                    //tmp.x-=num;                    for(int i=0;i<num;i++){                        --tmp.x;                        if(tmp.x<1||tmp.x>A||tmp.y<1||tmp.y>B){                            f=true;                            printf("Robot %d crashes into the wall\n",I);                            break;                        }else if(map[tmp.x][tmp.y]){                            f=true;                            printf("Robot %d crashes into robot %d\n",I,map[tmp.x][tmp.y]);                            break;                        }                    }                if(tmp.dir==1)                    //tmp.y+=num;                    for(int i=0;i<num;i++){                        ++tmp.y;                        if(tmp.x<1||tmp.x>A||tmp.y<1||tmp.y>B){                            f=true;                            printf("Robot %d crashes into the wall\n",I);                            break;                        }else if(map[tmp.x][tmp.y]){                            f=true;                            printf("Robot %d crashes into robot %d\n",I,map[tmp.x][tmp.y]);                            break;                        }                    }                if(tmp.dir==2)                    //tmp.x+=num;                    for(int i=0;i<num;i++){                        ++tmp.x;                        if(tmp.x<1||tmp.x>A||tmp.y<1||tmp.y>B){                            f=true;                            printf("Robot %d crashes into the wall\n",I);                            break;                        }else if(map[tmp.x][tmp.y]){                            f=true;                            printf("Robot %d crashes into robot %d\n",I,map[tmp.x][tmp.y]);                            break;                        }                    }                if(tmp.dir==3)                    //tmp.y-=num;                    for(int i=0;i<num;i++){                        --tmp.y;                        if(tmp.x<1||tmp.x>A||tmp.y<1||tmp.y>B){                            f=true;                            printf("Robot %d crashes into the wall\n",I);                            break;                        }else if(map[tmp.x][tmp.y]){                            f=true;                            printf("Robot %d crashes into robot %d\n",I,map[tmp.x][tmp.y]);                            break;                        }                    }            }            if(c[0]=='L')                tmp.dir=(tmp.dir-num+num*4)%4;            if(c[0]=='R')                tmp.dir=(tmp.dir+num)%4;            node[I]=tmp;            map[tmp.x][tmp.y]=I;        }        if(!f)            puts("OK");    }    return 0;}
0 0
原创粉丝点击