POJ 2632:crashing Robots

Source: Internet
Author: User

Crashing Robots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8424 Accepted: 3648

Description

In a modernized warehouse, robots is used to fetch the goods. Careful planning is needed to ensure, the robots reach their destinations without crashing into each of the other. Of course, all warehouses is rectangular, and all robots occupy a circular floor space with a diameter of 1 meter. Assume there is n robots, numbered from 1 through N. You'll get to know the position and orientation of each robot, and all the instructions, which is carefully (and mindle ssly) followed by the robots. Instructions is processed in the order they come. No. 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 both 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 integers, 1 <= A, B <=, giving the size of the warehouse I n meters. A is the length of the ew-direction, and B in the ns-direction.
The second line contains and integers, 1 <= N, M <=, denoting the numbers of robots and instructions Respectivel Y.
Then follow n lines with both integers, 1 <= Xi <= A, 1 <= Yi <= B and one letter (N, S, E or W), giving the St Arting position and direction of each robot, in order from 1 through N. No. Robots start at the same position.

Figure 1:the Starting positions of the robots in the sample warehouse
Finally There is M lines, giving the instructions in sequential order.
An instruction have the following format:
< robot #> < action> < repeat>
Where is one of
    • L:turn left degrees,
    • R:turn right degrees, or
    • F:move forward one meter,

and 1 <= < repeat> <= are the number of times the 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 are the moving Robot.
    • OK, if no crashing occurs.

Only the first crash are to be reported.

Sample Input

1 E5 4 W1 F 1 E5 4 1 E5 4 W1 F 1 4 W1 20 961 L E5 F-------+--F

Sample Output

Robot 1 crashes into the Wallrobot 1 crashes to Robot 2OKRobot 1 crashes into Robot 2

Test instructions is given a coordinate map, given a lot of robot coordinates and direction, there are three ways to operate the robot, F is forward, L is to turn left, R to the right. See which robot hit the wall first or crashed into another robot.

You need to pay attention to two points:

1. There is no need to operate the robot when the conflict, it may be in the beginning of the robot visit the location of the robot station has been repeated, that is, conflict.

2.R to the right, the definition of 1, the original thought that negative numbers divided by the remainder of the number will be positive, the result of their own too naive, to consider turning it into a positive.

This problem oneself notice these two points is enough, because itself is a simulation process, and the algorithm is not related ...

Code:

#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string > #include <cstring> #pragma warning (disable:4996) using namespace std;struct r{int x;int y;int dir;} robot[110];enum{e,n,w,s};enum{l=1,r=-1};int map_f[200][200];int move_x[5]={1,0,-1,0};int move_y[5]={0,1,0,-1};int Test,x,y,robot_n,q,i,flag;string temp;void Solve () {int cal_n,cishu,k;string manu;cin>>cal_n>>manu> >cishu;if (flag==0) return;if (manu== "F") {map_f[robot[cal_n].x][robot[cal_n].y]=0;for (k=1;k<=cishu;k++) { robot[cal_n].x = robot[cal_n].x + Move_x[robot[cal_n].dir]; ROBOT[CAL_N].Y = robot[cal_n].y + move_y[robot[cal_n].dir];if (robot[cal_n].x <=0 | | Robot[cal_n].y <=0 | | robot[cal_n].x >x | | Robot[cal_n].y >y) {flag=0;cout<< "Robot" <<cal_n<< "crashes into the Wall" <<endl;break;} else if (Map_f[robot[cal_n].x][robot[cal_n].y]) {flag=0;cout<< "Robot" <<cal_n<< "crashes into Robot "<<map_f[rObot[cal_n].x][robot[cal_n].y]<<endl;break;}} Map_f[robot[cal_n].x][robot[cal_n].y]=cal_n;} else if (manu== "L") {Robot[cal_n].dir = (Robot[cal_n].dir + L*cishu)%4;} else if (manu== "R") {Robot[cal_n].dir = (Robot[cal_n].dir + (R*cishu)%4 + 4)%4;}} int main () {cin>>test;while (test--) {Flag=1;memset (map_f,0,sizeof (map_f));cin>>x>>y;cin>> Robot_n>>q;for (i=1;i<=robot_n;i++) {cin>>robot[i].x>>robot[i].y;if (Map_f[Robot[i].x][Robot [I].Y]) {flag=0;cout<< "Robot" <<i<< "crashes into Robot" <<map_f[Robot[i].x][Robot[i].y]< <endl;} Else{map_f[robot[i].x][robot[i].y]=i;} Cin>>temp;if (temp== "E") Robot[i].dir=e;else if (temp== "N") Robot[i].dir=n;else if (temp== "W") robot[i].dir=w; else if (temp== "S") robot[i].dir=s;} for (i=1;i<=q;i++) {Solve ();} if (flag) cout<< "OK" &LT;&LT;ENDL;} System ("pause"); return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2632:crashing Robots

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.