首页 > 代码库 > POJ 2632 Crashing Robots

POJ 2632 Crashing Robots

Crashing Robots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7505 Accepted: 3279

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

4
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20

Sample Output

Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2

我讨厌模拟题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


AC代码如下:


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

struct H
{
    int h,z;
    char f;
}rt[105];

int t;
int n,m,g,s;
int x[105],y[105];
int map[105][105];
char f[105][105];
char d[105][2];
char b;
int a,c;
int bj;

void DS()//转向
{
    c=c%4;
    if(b=='R')//左右是对称的
        c=4-c;
    if(c==1)
    {
        if(rt[a].f=='N')
            rt[a].f='W';
        else if(rt[a].f=='E')
            rt[a].f='N';
        else if(rt[a].f=='S')
            rt[a].f='E';
        else if(rt[a].f=='W')
            rt[a].f='S';
    }
    else if(c==2)
    {
        if(rt[a].f=='N')
            rt[a].f='S';
        else if(rt[a].f=='E')
            rt[a].f='W';
        else if(rt[a].f=='S')
            rt[a].f='N';
        else if(rt[a].f=='W')
            rt[a].f='E';
    }
    else if(c==3)
    {
        if(rt[a].f=='N')
            rt[a].f='E';
        else if(rt[a].f=='E')
            rt[a].f='S';
        else if(rt[a].f=='S')
            rt[a].f='W';
        else if(rt[a].f=='W')
            rt[a].f='N';
    }

}

void DF()//前进函数
{
    int i;
    if(rt[a].f=='W')
    {
        int xx=rt[a].z-c;
            for(i=rt[a].z-1;i>=xx;i--)
            if(map[rt[a].h][i]!=0&&i>=1)
              {printf("Robot %d crashes into robot %d\n",a,map[rt[a].h][i]);bj=1;break;}
              if(xx<1&&bj==0)
           {printf("Robot %d crashes into the wall\n",a);bj=1;}
            if(bj==0)
               {map[rt[a].h][rt[a].z]=0;map[rt[a].h][xx]=a;rt[a].z=xx;}
    }
    else if(rt[a].f=='N')
    {

        int xx=rt[a].h+c;
            for(i=rt[a].h+1;i<=xx;i++)
            if(map[i][rt[a].z]!=0&&i<=n)
              {printf("Robot %d crashes into robot %d\n",a,map[i][rt[a].z]);bj=1;break;}
              if(xx>n&&bj==0)
           {printf("Robot %d crashes into the wall\n",a);bj=1;}
            if(bj==0)
               {map[rt[a].h][rt[a].z]=0;map[xx][rt[a].z]=a;rt[a].h=xx;}
    }
    else if(rt[a].f=='E')
    {

        int xx=rt[a].z+c;
            for(i=rt[a].z+1;i<=xx;i++)
            if(map[rt[a].h][i]!=0&&i<=m)
              {printf("Robot %d crashes into robot %d\n",a,map[rt[a].h][i]);bj=1;break;}
              if(xx>m&&bj==0)
           {printf("Robot %d crashes into the wall\n",a);bj=1;}
            if(bj==0)
               {map[rt[a].h][rt[a].z]=0;map[rt[a].h][xx]=a;rt[a].z=xx;}

    }
    else if(rt[a].f=='S')
    {

        int xx=rt[a].h-c;
            for(i=rt[a].h-1;i>=xx;i--)
            if(map[i][rt[a].z]!=0&&i>=1)
              {printf("Robot %d crashes into robot %d\n",a,map[i][rt[a].z]);bj=1;break;}
            if(xx<1&&bj==0)
           {printf("Robot %d crashes into the wall\n",a);bj=1;}
            if(bj==0)
               {map[rt[a].h][rt[a].z]=0;map[xx][rt[a].z]=a;rt[a].h=xx;}

    }
}

int main()
{
    int i,j;
   cin>>t;
    while(t--)
    {
        bj=0;
        memset(map,0,sizeof map);
        memset(f,0,sizeof f);
        cin>>m>>n>>g>>s;
        for(i=1;i<=g;i++)
        {
            cin>>y[i]>>x[i]>>d[i][0];
            map[x[i]][y[i]]=i;
            rt[i].h=x[i];rt[i].z=y[i];
            rt[i].f=d[i][0];
            f[x[i]][y[i]]=d[i][0];
            //cout<<map[x[i]][y[i]]<<f[x[i]][y[i]]<<endl;
        }
        for(i=1;i<=s;i++)
        {
            cin>>a>>b>>c;
            if(bj==1)
                continue;
            if(b=='L')
                DS();
            else if(b=='R')
                DS();
            else if(b=='F')
                DF();
            //cout<<a<<" "<<rt[a].h<<" "<<rt[a].z<<" "<<rt[a].f<<endl<<endl;
        }
        if(bj==0)
            cout<<"OK"<<endl;
    }
    return 0;
}