首页 > 代码库 > poj2632 模拟

poj2632 模拟

Crashing Robots

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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
#include<iostream>#include <string.h>#include <stdio.h>using namespace std;int main(void){    int cases;    int a,b;  ///a*b矩阵    int n,m;  ///机器人数&&询问次数    int x,y;  ///机器人坐标    char dire;  ///方向    int rob[101],rep[101];   ///编号&&重复次数    char act[101];          ///指令    bool flag=false;    int i,k;    int post[101][101];      ///初始位置    int num[101][101];       ///编号    int xx[101],yy[101];     ///编号    cin>>cases;    while(cases--)    {        memset(post,-1,sizeof(post));        memset(num,0,sizeof(num));        memset(xx,0,sizeof(xx));        memset(yy,0,sizeof(yy));        cin>>a>>b;        cin>>n>>m;        ///输入:        for(i=1;i<=n;i++)        {            cin>>y>>x>>dire;   //使用cin函数时,空字符不会作为字符输入到char            xx[i]=x;            yy[i]=y;                  //编号记录(编号作为下标,通过编号反搜坐标)            num[x][y]=i;              //编号记录(坐标作为下标,通过坐标反搜编号)            if(dire==S)             //方向记录(坐标作为下标,通过坐标反搜方向)                post[x][y]=0;        //0 or 360     【由于坐标系改变,注意上下颠倒,即N为南,S为北,但WE不变】            else if(dire==E)                post[x][y]=1;        //90            else if(dire==N)                post[x][y]=2;        //180            else if(dire==W)                post[x][y]=3;        //270        }                            //用0~3代替是为了减少运算次数        /*Input < robot #> < action> < repeat>*/        for(k=1;k<=m;k++)            cin>>rob[k]>>act[k]>>rep[k];        bool flag=false;        int row,col;        for(k=1;k<=m;k++)        {            row=xx[rob[k]];            col=yy[rob[k]];            if(act[k]==L)       //【由于坐标系改变,注意转向相反:L转右,R转左】            {                rep[k]%=4;   //角度每转4次(360度)回到原位                post[row][col] = ( post[row][col] + rep[k] ) % 4;            }            else if(act[k]==R)            {                rep[k]%=4;                post[row][col] = ( post[row][col] - rep[k] ) % 4;                if(post[row][col]<0)   //注意余数为负的情况                    post[row][col]+=4;            }            else if(act[k]==F)            {                if(post[row][col]==0)      //N方向移动判断                    for(i=1;i<=rep[k];i++)                    {                        if(num[row-i][col]) //如果该格存在编号(即存在另外的robot)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row-i][col]<<endl;                            flag=true;                            break;                        }                        if(row-i<1)  //判断是否撞墙                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {   //移动中若无任何碰撞,则把robot原坐标记录的信息更新到新坐标                            post[row][col]=-1;   //原坐标方向重置                            num[row][col]=0;      //原坐标编号重置                            row-=rep[k];                            xx[rob[k]]-=rep[k];                            post[row][col]=0;    //新坐标方向更新                            num[row][col]=rob[k];   //新坐标编号更新                        }                    }                else if(post[row][col]==1)  //E方向移动判断                    for(i=1;i<=rep[k];i++)                    {                        if(num[row][col+i])                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col+i]<<endl;                            flag=true;                            break;                        }                        if(col+i>a)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {                            post[row][col]=-1;                            num[row][col]=0;                            col+=rep[k];                            yy[rob[k]]+=rep[k];                            post[row][col]=1;                            num[row][col]=rob[k];                        }                    }                else if(post[row][col]==2)  //S方向移动判断                    for(i=1;i<=rep[k];i++)                    {                        if(num[row+i][col])                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row+i][col]<<endl;                            flag=true;                            break;                        }                        if(row+i>b)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {                            post[row][col]=-1;                            num[row][col]=0;                            row+=rep[k];                            xx[rob[k]]+=rep[k];                            post[row][col]=2;                            num[row][col]=rob[k];                        }                    }                else if(post[row][col]==3)  //W方向移动判断                    for(i=1;i<=rep[k];i++)                    {                        if(num[row][col-i])                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col-i]<<endl;                            flag=true;                            break;                        }                        if(col-i<1)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {                            post[row][col]=-1;                            num[row][col]=0;                            col-=rep[k];                            yy[rob[k]]-=rep[k];                            post[row][col]=3;                            num[row][col]=rob[k];                        }                    }            }            if(flag)break;        }        if(!flag)            cout<<"OK"<<endl;    }    return 0;}

注释: 题意可以读懂  

         坐标的问题很严肃

        还没有A   下面是我错的代码

Virtual Judge    Problem    Status    Contest    Discuss    Task    ToolOJ:ProbID:Result:Username:OJ Prob ID:RunID     Problem     Result     Time     Memory     Language     Length     User     Sumit Time855     POJ 2632     Accepted        16 MS     736 KB     G++     4885 B     zylp     2014-07-18 13:51:44854     POJ 2632     Wrong Answer        0 MS     0 KB     G++     6431 B     zylp     2014-07-18 13:51:10790     POJ 1068     Accepted        0 MS     688 KB     G++     1289 B     zylp     2014-07-18 10:06:46789     POJ 1068     Presentation Error        0 MS     0 KB     G++     1272 B     zylp     2014-07-18 10:05:38    ← Previous Page    Top Page    Next Page →Source Code : POJ 2632, RunID : 854#include <iostream>#include <string.h>#include <stdio.h>using namespace std;int main(){    int k,a,b,n,m,i;    int x,y; ///机器人的位置    char dir;  ///机器人的方向    int rob[101],rep[101];    char act[101];    int xx[101],yy[101];    int num[101][101]; ///机器人的编号    int pos[101][101]; ///机器人的方向    cin>>k;    while(k--)    {        ///前提        memset(xx,0,sizeof(xx));        memset(yy,0,sizeof(yy));        memset(num,0,sizeof(num));        memset(pos,-1,sizeof(pos));        ///输入        cin>>a>>b;        cin>>n>>m;        for(int i=1; i<=n; i++)        {            cin>>x>>y>>dir;            xx[i]=x;            yy[i]=y;            num[x][y]=i;            if(dir==S)                pos[x][y]=0;            if(dir==E)                pos[x][y]=1;            if(dir==N)                pos[x][y]=2;            if(dir==W)                pos[x][y]==3;        }        for(int k=1; k<=m; k++)        {            cin>>rob[k]>>act[k]>>rep[k];        }        int row,col;        bool flag=false;        ///处理        for(int k=1; k<=m; k++)        {            row=xx[rob[k]];            col=yy[rob[k]];            if(act[k]==L)            {                rep[k]=rep[k]%4;                pos[row][col]=(pos[row][col]+rep[k])%4;            }            if(act[k]==R)            {                rep[k]=rep[k]%4;                pos[row][col]=(pos[row][col]-rep[k])%4;                if(pos[row][col]<0)                    pos[row][col]+=4;            }            if(act[k]==F)            {                if(pos[row][col]==0)                {                    for(i=1; i<=rep[k]; i++)                    {                        if(num[row-i][col]) //如果该格存在编号(即存在另外的robot)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row-i][col]<<endl;                            flag=true;                            break;                        }                        if(row-i<1)  //判断是否撞墙                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {                            //移动中若无任何碰撞,则把robot原坐标记录的信息更新到新坐标                            pos[row][col]=-1;   //原坐标方向重置                            num[row][col]=0;      //原坐标编号重置                            row-=rep[k];                            xx[rob[k]]-=rep[k];                            pos[row][col]=0;    //新坐标方向更新                            num[row][col]=rob[k];   //新坐标编号更新                        }                    }                }     //N方向移动判断                else if(pos[row][col]==1)  //E方向移动判断                    for(i=1; i<=rep[k]; i++)                    {                        if(num[row][col+i])                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col+i]<<endl;                            flag=true;                            break;                        }                        if(col+i>a)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {                            pos[row][col]=-1;                            num[row][col]=0;                            col+=rep[k];                            yy[rob[k]]+=rep[k];                            pos[row][col]=1;                            num[row][col]=rob[k];                        }                    }                else if(pos[row][col]==2)  //S方向移动判断                    for(int i=1; i<=rep[k]; i++)                    {                        if(num[row+i][col])                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row+i][col]<<endl;                            flag=true;                            break;                        }                        if(row+i>b)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {                            pos[row][col]=-1;                            num[row][col]=0;                            row+=rep[k];                            xx[rob[k]]+=rep[k];                            pos[row][col]=2;                            num[row][col]=rob[k];                        }                    }                else if(pos[row][col]==3)  //W方向移动判断                    for(i=1; i<=rep[k]; i++)                    {                        if(num[row][col-i])                        {                            cout<<"Robot "<<num[row][col]<<" crashes into robot "<<num[row][col-i]<<endl;                            flag=true;                            break;                        }                        if(col-i<1)                        {                            cout<<"Robot "<<num[row][col]<<" crashes into the wall"<<endl;                            flag=true;                            break;                        }                        if(i==rep[k])                        {                            pos[row][col]=-1;                            num[row][col]=0;                            col-=rep[k];                            yy[rob[k]]-=rep[k];                            pos[row][col]=3;                            num[row][col]=rob[k];                        }                    }            }        }        if(!flag)        cout<<"OK"<<endl;    }    return 0;}execute time:0.002085ms, 2 db queris.    Version: HRBUST 1024 Beta    ·    HRBUST OJ