Crashing Robots(水题,模拟)

Posted htmrc1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Crashing Robots(水题,模拟)相关的知识,希望对你有一定的参考价值。

1020: Crashing Robots

时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte

总提交: 207            测试通过:101

描述

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.

输入

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 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.

样例输入

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

样例输出

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

题意:在一个长A宽B的矩形房间内有n个机器人,给m条指令判断机器人是否会和墙相撞或是否和其他机器人相撞。

此题就是纯粹的模拟,先把4个方向改为数字存,每次移动并记录,注意往左和右移。注意每一次移动都要判断是否相撞,而不是最后指令全部执行才判断。

 1 #include "iostream"
 2 using namespace std;
 3 int a,b,n,m;
 4 struct wxl
 5 {
 6     int x,y;
 7     int s;
 8 }ht[110];
 9 bool panduan(int h)//判断机器人是否会相撞 
10 {
11     if(ht[h].x<=0||ht[h].x>a||ht[h].y<=0||ht[h].y>b)
12     {
13         cout<<"Robot "<<h<<" crashes into the wall"<<endl;
14         return false;
15     }
16     for(int i=1;i<=n;i++)
17     {
18         if(i==h)continue;
19         if(ht[i].x==ht[h].x&&ht[i].y==ht[h].y)
20         {
21             cout<<"Robot "<<h<<" crashes into robot "<<i<<endl;
22             return false;
23         }
24     }
25     return true;
26 }
27 int main()
28 {
29     int i,t,p,j,k;
30     bool flag;
31     string str;
32     cin>>k;
33     while(k--)
34     {
35         cin>>a>>b>>n>>m;
36         for(i=1;i<=n;i++)//将方向转为数字存 
37         {
38             cin>>ht[i].x>>ht[i].y>>str;
39             if(str=="N")ht[i].s=0;
40             else if(str=="E")ht[i].s=1;
41             else if(str=="S")ht[i].s=2;
42             else if(str=="W")ht[i].s=3;
43         }
44         flag=true;//开始全为不会相撞 
45         for(i=0;i<m;i++)
46         {
47             cin>>t>>str>>p;//t为机器人,str表示往哪个方向,p表示前进几步 
48             if(str=="F")//F最为简单,先判断 
49             {
50                 if(flag)
51                 {
52                     for(j=0;j<p;j++)
53                     {
54                         if(ht[t].s==0)ht[t].y++;
55                         if(ht[t].s==1)ht[t].x++;
56                         if(ht[t].s==2)ht[t].y--;
57                         if(ht[t].s==3)ht[t].x--;
58                         flag=panduan(t);//此处开始判断 
59                         if(!flag)break;//因为在函数中写了输出函数,此处直接退出 
60                     }
61                 }
62             }
63             else if(str=="L")
64             {
65                 ht[t].s=(ht[t].s-p%4+4)%4;//注意 
66             }
67             else if(str=="R")
68             {
69                 ht[t].s=(ht[t].s+p%4)%4;
70             }
71         }
72         if(flag)cout<<"OK"<<endl;
73     }
74 }

 











































以上是关于Crashing Robots(水题,模拟)的主要内容,如果未能解决你的问题,请参考以下文章

poj 2632 Crashing Robots模拟略坑~

POJ 2632 -- Crashing Robots

二分图匹配入门专题1I - Hiding Gold light oj 1152二分图匹配-------------------我是终于不那么水的水题分割线------------------(代码片

__CRASHING_DUE_TO_PRIVACY_VIOLATION__

MOCTF - WriteUp

CodeForces 709A Juicer (水题, 模拟)