本文共 4330 字,大约阅读时间需要 14 分钟。
Crashing Robots
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11971 | Accepted: 5063 |
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,
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.
Sample Input
45 42 21 1 E5 4 W1 F 72 F 75 42 41 1 E5 4 W1 F 32 F 11 L 11 F 35 42 21 1 E5 4 W1 L 961 F 25 42 31 1 E5 4 W1 F 41 L 11 F 20
Sample Output
Robot 1 crashes into the wallRobot 1 crashes into robot 2OKRobot 1 crashes into robot 2
Source
解题思路:
简单的模拟而已。。。程序很长不是因为算法(根本就没算法= =)而是因为很多情况要考虑,要有耐心需要小心的是,当坐标系变换后,注意方向的改变规律注意事项: 1、坐标系要改变为二维矩阵的形式,N、W、S、E的方向变化必须注意:改变坐标系后,N为南,S为北,WE不变,L转右,R转左,F不变; 2、对于求余数处理是否注意出现负数的情况; 3、robot移动过程中,crashes robot和crashes wall 同时判断,crashes robot放在前面。
代码:
#include#include using namespace std;int x[105];int y[105];int c[105];bool crash;bool wall(int robotNum, int a, int b){ if(x[robotNum] <= 0 || x[robotNum] > a || y[robotNum] <= 0 || y[robotNum] > b) return true; return false;}int detectCrash(int robotNum, int a, int b, int n){ for(int i = 0; i < n; i++) { if(i != robotNum ) { if(x[i] == x[robotNum] && y[i] == y[robotNum]) return i; } } return -1; }void ford(int robotNum, int a, int b, int n){ switch(c[robotNum]) { case 0: x[robotNum]--; break; case 1: y[robotNum]--; break; case 2: x[robotNum]++; break; case 3: y[robotNum]++; break; } if(wall(robotNum, a, b)) { cout<<"Robot "< <<" crashes into the wall"< >k; while(k--) { memset(x, 0, sizeof(x)); memset(y, 0, sizeof(y)); memset(c, 0, sizeof(c)); int a, b; cin>>a>>b; //宽 高 int n, m; cin>>n>>m; //机器人数 指令数 crash = false; for(int i = 0; i < n; i++) { char cc; cin>>x[i]>>y[i]>>cc; if(cc == 'W') c[i] = 0; else if(cc == 'S') c[i] = 1; else if(cc == 'E') c[i] = 2; else if(cc == 'N') c[i] = 3; } for(int i = 0; i < m; i++) { int robot, repeat; char action; cin>>robot>>action>>repeat; for(int j = 0; j < repeat; j++) { switch(action) { case 'L': c[robot-1]++; if(c[robot-1] == 4) c[robot-1] = 0; break; case 'R': c[robot-1]--; if(c[robot-1] == -1) c[robot-1] = 3; break; case 'F': if(!crash) //如果发生过crash, 就没有必要再根据后面的指令了操作了 { ford(robot-1, a, b, n); } break; } } } if(!crash) { cout<<"OK"<
转载地址:https://joycez.blog.csdn.net/article/details/82791372 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!