码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2632 Crashing Robots(模拟)

时间:2017-07-22 19:37:13      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:iam   sub   ref   判断   orm   void   less   style   mod   

题目网址:http://poj.org/problem?id=2632

题目:

Crashing Robots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10880   Accepted: 4614

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。需要注意的是方向按图中的设置,还有就是不要直接在当前位置加上步数来判断,有可能走到一半就碰到别的机器人了,所以要一步步走。

代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 struct node{
 6     int x,y;
 7     int d;
 8 }robot[105];
 9 struct direct{
10     int x,y;
11 }dir[4]={{1,0},{0,-1},{-1,0},{0,1}};
12 int vis[105][105];
13 int t,l,w,n,m;
14 void input(){
15     cin>>l>>w>>n>>m;
16     for (int i=1; i<=n; i++) {
17         node r;
18         char ch;
19         cin>>r.x>>r.y>>ch;
20         vis[r.x][r.y]=i;
21         switch (ch) {
22             case E:
23                 r.d=0;
24                 break;
25             case S:
26                 r.d=1;
27                 break;
28             case W:
29                 r.d=2;
30                 break;
31             case N:
32                 r.d=3;
33                 break;
34             default:
35                 break;
36         }
37         robot[i]=r;
38     }
39 }
40 int main(){
41     scanf("%d",&t);
42     while (t--) {
43         int ok=1;
44         memset(vis, 0, sizeof(vis));
45         input();
46         for (int i=0; i<m; i++) {
47             int number,tim;
48             char op;
49             cin>>number>>op>>tim;
50             if(!ok) continue;
51             if(op==F){
52                 for(int j=0;j<tim && ok;j++){
53                     int x=robot[number].x+dir[robot[number].d].x;
54                     int y=robot[number].y+dir[robot[number].d].y;
55                     if(x<=0 || x>l || y<=0 || y>w)    {
56                         ok=0;
57                         printf("Robot %d crashes into the wall\n",number);
58                     }
59                     else if(vis[x][y]){
60                         ok=0;
61                         printf("Robot %d crashes into robot %d\n",number,vis[x][y]);
62                     }
63                     else{
64                         vis[robot[number].x][robot[number].y]=0;
65                         vis[x][y]=number;
66                         robot[number].x=x,robot[number].y=y;
67                     }
68                 }
69             }
70             else if(op==L)    robot[number].d=(robot[number].d-tim%4+4)%4;
71             else if(op==R)    robot[number].d=(robot[number].d+tim%4)%4;
72         }
73         if(ok)  printf("OK\n");
74     }
75     return 0;
76 }

 

 

POJ 2632 Crashing Robots(模拟)

标签:iam   sub   ref   判断   orm   void   less   style   mod   

原文地址:http://www.cnblogs.com/uniles/p/7222069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!