hdu 2300 Crashing Robots(模拟)

来源:互联网 发布:chalea高清网络电视 编辑:程序博客网 时间:2024/05/16 11:06

Crashing Robots

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 830    Accepted Submission(s): 312


Problem Description
In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occupy a circular floor space with a diameter of 1 meter. Assume there are N robots, numbered from 1 through N. You will get to know the position and orientation of each robot, and all the instructions, which are carefully (and mindlessly) followed by the robots. Instructions are processed in the order they come. No two robots move simultaneously; a robot always completes its move before the next one starts moving. 
A robot crashes with a wall if it attempts to move outside the area of the warehouse, and two robots crash with each other if they ever try to occupy the same spot.

 

Input
The first line of input is K, the number of test cases. Each test case starts with one line consisting of two integers, 1 <= A, B <= 100, giving the size of the warehouse in meters. A is the length in the EW-direction, and B in the NS-direction. 
The second line contains two integers, 1 <= N, M <= 100, denoting the numbers of robots and instructions respectively. 
Then follow N lines with two integers, 1 <= Xi <= A, 1 <= Yi <= B and one letter (N, S, E or W), giving the starting position and direction of each robot, in order from 1 through N. No two robots start at the same position. 


Figure 1: The starting positions of the robots in the sample warehouse

Finally there are M lines, giving the instructions in sequential order. 
An instruction has the following format: 
< robot #> < action> < repeat> 
Where is one of 

L: turn left 90 degrees, 

R: turn right 90 degrees, or 

F: move forward one meter,

and 1 <= < repeat> <= 100 is the number of times the robot should perform this single move.
 

Output
Output one line for each test case: 

Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.) 

Robot i crashes into robot j, if robots i and j crash, and i is the moving robot. 

OK, if no crashing occurs.

Only the first crash is to be reported.
 

Sample Input
45 42 21 1 E5 4 W1 F 72 F 75 42 41 1 E5 4 W1 F 32 F 11 L 11 F 35 42 21 1 E5 4 W1 L 961 F 25 42 31 1 E5 4 W1 F 41 L 11 F 20
 

Sample Output
Robot 1 crashes into the wallRobot 1 crashes into robot 2OKRobot 1 crashes into robot 2


题意:

给出一个矩形和若干个机器人坐标和初始方向,给出一些指令。如果机器人撞墙输出一类,碰到另一个机器人输出一类,若都没有则输出OK。


思路:

机器人是按照顺序依次执行的,所以按照顺序来模拟。
唯一要注意的地方是,输入x,y对应的是图中的坐标系。而输入进mp[][]与图中的不一样需要变换一下。
q[i].x=b-y+1,q[i].y=x;  这样是以图中的-y为x轴,x为y轴,但一样能模拟出来。

代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=105;int mp[maxn][maxn];struct rob{    int x,y;    char dir[4];}q[maxn];int a,b,n,m;void turn(int i,char dir){    if(dir=='L')    {        if(q[i].dir[0]=='N')            q[i].dir[0]='W';        else if(q[i].dir[0]=='W')            q[i].dir[0]='S';        else if(q[i].dir[0]=='S')            q[i].dir[0]='E';        else            q[i].dir[0]='N';    }    else if(dir=='R')    {        if(q[i].dir[0]=='N')            q[i].dir[0]='E';        else if(q[i].dir[0]=='E')            q[i].dir[0]='S';        else if(q[i].dir[0]=='S')            q[i].dir[0]='W';        else            q[i].dir[0]='N';    }}int mov(int i){    char dir=q[i].dir[0];    if(dir=='N')    {        if(q[i].x-1<1)        {            return 1;        }        else if(mp[q[i].x-1][q[i].y]!=0)        {            return 2;        }        else        {            mp[q[i].x][q[i].y]=0;            mp[--q[i].x][q[i].y]=i;        }    }    else if(dir=='S')    {        if(q[i].x+1>b)        {            return 1;        }        else if(mp[q[i].x+1][q[i].y]!=0)        {            return 2;        }        else        {            mp[q[i].x][q[i].y]=0;            mp[++q[i].x][q[i].y]=i;        }    }    else if(dir=='W')    {        if(q[i].y-1<1)        {            return 1;        }        else if(mp[q[i].x][q[i].y-1]!=0)        {            return 2;        }        else        {            mp[q[i].x][q[i].y]=0;            mp[q[i].x][--q[i].y]=i;        }    }    else if(dir=='E')    {        if(q[i].y+1>a)        {            return 1;        }        else if(mp[q[i].x][q[i].y+1]!=0)        {            return 2;        }        else        {            mp[q[i].x][q[i].y]=0;            mp[q[i].x][++q[i].y]=i;        }    }    return 0;}int geti(int i){    char dir=q[i].dir[0];    if(dir=='N')    {        if(mp[q[i].x-1][q[i].y]!=0)        {            return mp[q[i].x-1][q[i].y];        }    }    else if(dir=='S')    {        if(mp[q[i].x+1][q[i].y]!=0)        {            return mp[q[i].x+1][q[i].y];        }    }    else if(dir=='W')    {        if(mp[q[i].x][q[i].y-1]!=0)        {            return mp[q[i].x][q[i].y-1];        }    }    else    {        if(mp[q[i].x][q[i].y+1]!=0)        {            return mp[q[i].x][q[i].y+1];        }    }    return 0;}int main(){    int t;    scanf("%d",&t);    int num,rep;    char act[4];    while(t--)    {        memset(mp,0,sizeof(mp));        scanf("%d%d",&a,&b);        scanf("%d%d",&n,&m);        int x,y;        for(int i=1;i<=n;i++)        {            cin>>x>>y>>q[i].dir;            q[i].x=b-y+1,q[i].y=x;            mp[q[i].x][q[i].y]=i;        }        int flag=-1,jii,jij;        int outt=-1;        for(int i=1;i<=m;i++)        {            cin>>num>>act>>rep;            if(outt!=-1)                continue;            for(int j=1;j<=rep;j++)            {                if(outt!=-1)                    continue;                if(act[0]=='L'||act[0]=='R')                {                    turn(num,act[0]);                }                else if(act[0]=='F')                {                    int k=mov(num);                    if(k==1)                    {                        flag=1;                        jii=num;                        outt=1;                    }                    else if(k==2)                    {                        flag=2;                        jii=num;                        outt=1;                    }                }            }        }        if(flag==-1)        {            printf("OK\n");        }        else if(flag==1)        {            printf("Robot %d crashes into the wall\n",jii);        }        else        {            int k=geti(jii);            printf("Robot %d crashes into robot %d\n",jii,k);        }    }    return 0;}