1. 程式人生 > >Crashing Robots(水題,模擬)

Crashing Robots(水題,模擬)

nes 純粹 rst finall gpo without diameter als tar

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(水題,模擬)