Time Limit: 3.000 seconds
Time Limit: 3.000 seconds
Background
Background
Robotics, Robot Motion Planning, and machine learning are areas that cross the boundaries of ideas of the Subdisciplines that comprise Computer Science: artificial intelligence, algorithms and complexity, electrical and Mechanical Engineering to name a few. in addition, robots as "turtles" (lost Red by work by Papert, Abelson, and disallow) and as "beeper-pickers" (lost Red by work by Pattis) have been studied and used by students as an introduction to programming for policyears.
Robotics, Robot Motion Planning, and machine learning span many sub-disciplines in computer science, including artificial intelligence,AlgorithmAnd complexity, electronic and mechanical engineering, just a few examples. What's more, "turtles" robots (created by Papert, Abelson, and disallow) and "beeching and picking" robots? (Created by Pattis) has been used for research and programming for many years.
This problem involves determining the position of a robot indexing a pre-Columbian flat world.
This problem is to allow robots to explore a "pre-Columbus era" (before Columbus discovered the new world) in a flat world and determine the location of the process.
The Problem
Problem
Given the dimensions of a rectangular grid and a sequence of robot positions and instructions, you are to write a program that determines for each sequence of robot positions and instructions the final position of the robot.
Given a rectangular grid and a group of robot location information and instructions, you need to writeProgramTo determine the final position of the robot when each group of location information and instructions are executed.
A robot position consists of a grid coordinate (a pair of integers: X-coordinate followed by Y-coordinate) and an orientation (N, S, E, W for North, South, east, and west ). A robot instruction is a string of the letters 'l', 'R', and 'F' which represent, respectively, the instructions:
The location information of a robot consists of a grid coordinate (an integer: X following y) and a direction value (N, S, E, W corresponds to north, south, east, and west). A robot instruction is a string consisting of letters "L", "r", and "F". The instructions are as follows:
- Left: the robot turns left 90 degrees and remains on the current grid point.
Left: the robot rotates 90 degrees to the left and keeps the original grid position unchanged.
- Right: The robot turns right 90 degrees and remains on the current grid point.
Right: The robot rotates 90 degrees to the right and keeps the original grid position unchanged.
- Forward: the robot moves forward one grid point in the direction of the current orientation and mantains the same orientation.
Forward: the robot moves forward in the current direction and remains unchanged in the original direction.
The direction north corresponds to the direction from grid point (x, y) to grid point (X, Y + 1 ).
The direction of "North" is from grid coordinates (x, y) to grid coordinates (x, y + 1 ).
Since the grid is rectangular and bounded, a robot that moves "off" an edge of the grid is lost forever. however, lost robots leave a robot "scent" that prohibits future robots from dropping off the world at the same grid point. the scent is left at the last grid position the robot occupied before disappearing over the edge. an instruction to move "off" the world from a grid point from which a robot has been previusly lost is simply ignored by the current robot.
The grid is rectangular and bounded. When a robot moves out of the edge of the grid, it will lose forever. However, the lost robots will leave some "information elements" to prevent later robots from falling from the same grid. The information element remains in the front of a robot that disappears out of the edge. If an instruction is sent to remove a robot from a grid coordinate, but a robot has been removed from the coordinate and lost, the instruction should be ignored.
The input
Input
The first line of input is the upper-right coordinates of the rectangular world, the lower-left coordinates are assumed to be 0, 0.
The first line of the input is the coordinates in the upper right corner of the rectangular world, and the coordinates in the lower left corner are set to (0, 0 ).
The remaining input consists of a sequence of robot positions and instructions (two lines per robot ). A position consists of two integers specifying the initial coordinates of the robot and an orientation (N, S, E, W), all separated by white space on one line. A robot instruction is a string of the letters 'l', 'R', and 'F' on one line.
Other inputs consist of a set of robot location information and instructions (each Robot occupies two lines ). Location Information is composed of two integers indicating the initial coordinates of the robot and one direction value (N, S, E, W.
Each robot is processed sequentially, I. e., finishes executing the robot instructions before the next robot begins execution.
Each robot must be processed in sequence, that is, the current robot must be executed before the next robot starts to execute.
Input is terminated by end-of-file.
The input ends with EOF.
You may assume that all initial robot positions are within the bounds of the specified grid. The maximum value for any coordinate is 50. All struction strings will be less than 100 characters in length.
You can think that the initial coordinates of all robots are within the limited grid range. The maximum coordinate value is 50. All command strings are less than 100 characters in length.
The output
Output
For each robot position/instruction in the input, the output shocould indicate the final grid position and orientation of the robot. if a robot falls off the edge of the grid the word "lost" shocould be printed after the position and orientation.
Corresponding to the input of the coordinates/commands of each row of the robot, the final coordinates and directions of the robot should be output. If a robot drops from the edge of the grid, the "lost" should be output after the output coordinates and directions ".
Sample Input
Input example
5 3
1 1 E
Rfrfrf
3 2 N
Frrfllffrrfll
0 3 W
Llffflflfl
Sample output
Output example
1 1 E
3 3 N lost
2 3 S
Analysis
Analysis
Another simulated question is easier than #101. The only thing to note is that a robot can move into an existing grid with "information elements", but cannot remove borders from the grid. Algorithm inCodeThe annotations are clear, and there is no way to be clearer.
Solution
Answer
# Include <algorithm> # include <iostream> # include <vector> using namespace STD; struct point {int X; int y ;}; int main (void) {// vecscent is used to record the information left by the previously lost robot vector <int> vecscent; // szins is the instruction set, and szori is the direction conversion table char szins [100]. szori [4] = {'n', 'E', 's', 'w'}; // ptsize records the map size, and ptpos records the current position, ptori is the corresponding direction offset point ptsize, ptpos, ptori [4] = {0, 1}, {1, 0}, {0,-1}, {-1, 0 }}; // enter the map size CIN> ptsize. x> ptsize. y; // cyclically process each input robot F Or (char Cori; CIN> ptpos. x> ptpos. y> Cori> szins;) {// determines the number corresponding to the initial direction. Int Nori = find (& szori [0], & szori [4], Cori) -& szori [0], I = 0; // process each step in a loop for (; szins [I]! = 0; ++ I) {// turn left or right IF (szins [I]! = 'F') {Nori = (Nori + (szins [I] = 'l '? 3: 1) % 4; continue;} // move forward, calculate the coordinate point to be moved ptnew = {ptpos. X + ptori [NORI]. x, ptpos. Y + ptori [NORI]. y}; // if this movement does not cause any loss, save the new coordinate if (ptnew. x> = 0 & ptnew. x <= ptsize. X & ptnew. y> = 0 & ptnew. Y <= ptsize. y) {ptpos = ptnew; continue;} // otherwise, encode int nscent = ptpos based on the position and direction before the loss. y * 51 + ptpos. x; // + Nori * 51*51; // In the history, check whether a robot has lost this vector <int>: iterator iend = vecscent. end (); // if no loss exists, the current robot if (find (Vecscent. begin (), iend, nscent) = iend) {// record the loss information vecscent. push_back (nscent); break ;}// output result cout as required <ptpos. x <''<ptpos. Y <''<szori [NORI]; cout <(szins [I] = 0 )? "": "Lost") <Endl;} return 0 ;}