Crashing Robots(模拟法)

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

Source

Nordic 2005

题意:一个n*m的地图中,有若干个机器人,经过若干步操作,若机器人没有发生碰撞或者走出地图(就是碰到地图的边界)就输出OK,如果机器人走出地图输出"Robot 1 crashes into the wall"其中1为走出边界的机器人的编号;如果两个机器人相撞则输出"Robot 1 crashes into robot 2"表示机器人1撞了机器人2. 输入:第一行为T;代表有T组数据,第二行有两个数,代表地图的大小n*m,第三行有两个数,分别代表机器人的数量nn和操作指令的数目mm。4到nn+4行代表nn个机器人的位置和行动的方向。nn+5行到nn+5+m为操作指令。输入结束。 注意:并不是所有的机器人同时行动,而是根据操作指令一步一步的进行。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
   int x,y,d;
}s[110];
int A,B,N,M;
int i,j,mp[110][110],num,step;
char c;
int panduan()//通过数字来进行转向,按题目给定的方向建坐标
{
   if(c=='N')
    return 0;
   else if(c=='E')
    return 1;
   else if(c=='S')
    return 2;
   else if(c=='W')
    return 3;
}
void move()
{  int flag=1;
   int out,out1;
   for(i=1;i<=M;i++)
   {
       cin>>num>>c>>step;
       if(flag==1)
       {
          if(c=='L')
          {
             step%=4;
             step=4-step;//为保证无负数,减去step与加上4-step是一个方向
             s[num].d+=step;
             s[num].d%=4;
          }
          else if(c=='R')
          {
              step%=4;
              s[num].d+=step;
              s[num].d%=4;
          }
          else
          {
              int nowx,nowy;
              mp[s[num].x][s[num].y]=0;
             for(j=1;j<=step;j++)
             {
                 if(s[num].d==0)
                 {
                    nowx=s[num].x;
                    nowy=s[num].y+1;
                 }
                 else if(s[num].d==1)
                 {
                    nowx=s[num].x+1;
                    nowy=s[num].y;
                 }
                 else if(s[num].d==2)
                 {
                    nowx=s[num].x;
                    nowy=s[num].y-1;
                 }
                 else if(s[num].d==3)
                 {
                    nowx=s[num].x-1;
                    nowy=s[num].y;
                 }
                 if(mp[nowx][nowy]!=0)//该位置上有其它机器人
                 {
                     flag=-1;
                     out=num;
                     out1=mp[nowx][nowy];
                 }
                 else if(nowx>A||nowx<1||nowy<1||nowy>B)
                 {
                    flag=0;
                    out=num;
                 }

                 else
                 {
                     s[num].x=nowx;//记录下一步的位置
                     s[num].y=nowy;
                 }
             }
             mp[s[num].x][s[num].y]=num;//第num个机器人走到了该位置
          }
       }
   }
   if (flag==1)
    {
        cout<<"OK"<<endl;
    }
    else if (flag==-1)
    {
        cout<<"Robot " <<out<< " crashes into robot "<<out1<<endl;
    }
    else
    {
        cout<<"Robot " <<out<< " crashes into the wall"<<endl;
    }
}
int main()
{
    int u,v,k;
    cin>>k;
    while(k--)
    {
        memset(mp,0,sizeof(mp));
        memset(s,-1,sizeof(s));
        cin>>A>>B;
        cin>>N>>M;
        for(i=1;i<=N;i++)
        {
            cin>>u>>v;
            mp[u][v]=i;//第i个机器人在该位置上
            s[i].x=u;//第i个机器人的横纵坐标
            s[i].y=v;
            cin>>c;
            s[i].d=panduan();
        }
        move();

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值