Robot MotionTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7825 Accepted Submission (s): 3582
Problem Description
A robot have been programmed to follow the instructions in its path. Instructions for the next direction the robot was to move was laid down in a grid. The possible instructions is
N North (up the page)
S South (down the page)
E East (to "right" on the page)
W West (to the left on the page)
For example, suppose the robot starts on the north (top) side of grids 1 and starts south (down). The path the robot follows is shown. The robot goes through instructions in the grid before leaving the grid.
Compare What happens on Grid 2:the robot goes through 3 instructions only once, and then starts a loop through 8 instruct Ions, and never exits.
You is to write a program this determines how long it takes a robot to get out of the grid or how the robot loops around.
Inputthere'll be is one or more grids for robots to navigate. The data for each are in the following form. On the first line is three integers separated by blanks:the number of rows in the grid and the number of columns in the GRI D, and the number of the column in which the robot enters from the north. The possible entry columns is numbered starting with one in the left. Then come the rows of the direction instructions. Each grid would has at least one and at the most rows and columns of instructions. The lines of instructions contain only is the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Outputfor each grid with the input there is a line of output. Either the robot follows a certain number of instructions and exits the grid on any one, the four sides or else the robot F Ollows the instructions on a certain number of locations once, and then the instructions on some number of locations Repea tedly. The sample input below corresponds to the grids above and illustrates the both forms of output. The word "step" is all immediately followed by "(s)" Whether or not, the number before it is 1.
Sample Input
Sample Output
Step (s) to exit3 step (s) before a loop of 8 step (s)
Sourcemid-central USA 1999
Recommendwe carefully selected several similar problems for you:1016 1010 2553 1258 1045
A daily water, feel the brain is not working when you do the problem ....
The main idea of the topic is: give you a robot, and a map, this map is made up of letters, ' N ', ' S ', E ', ' W ', composition, when the robot encounters N and goes north, when you encounter S, go south, when you encounter E, go east, when you encounter W, go west, ask you how many steps the robot walked out of the border, or if the robot can't walk out of the Border 2, (that is, into the dead loop), find out the robot in the death cycle before the pace and the robot and how many steps of death Cycle.
Title input: Number of map rows, number of map columns, start of the robot entry (start), (the robot enters from the first line each time, the beginning of the input is the No. 0 row start-1 column)
Then someone might think of Dfs, the memory array. Although these bloggers have been contacted but also master not good, in fact, the problem with a simple simulation on the line ... Then do not do too much explanation, the topic is relatively simple, the code is as follows comments .....
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;char map[20][20]; Access map int step[20][20]; Record the total number of steps in the current walk int n,m,start;bool is_inmap (int x,int y)//Determine if you are out of bounds {if (! X>=0&&y>=0&&x<n&&y<m)) {return false; } else {return true; }}int Main () {while (scanf ("%d%d", &n,&m) &&n) {scanf ("%d", &start); memset (map,0,sizeof (map)); memset (step,-1,sizeof (step)); This sets each value in the array to 1 for (int i=0;i<n;i++) scanf ("%s", Map[i]); int anssteps = 0; The total number of steps used to access the walk step[0][start-1]=0; int x=0,y=start-1; Record the coordinates of the starting point while (1) {if (map[x][y]== ' N ') {x--; } else if (map[x][y]== ' S ') {x + +; } else if (map[x][y]== ' E ') {y++; } else if (map[x][y]== ' W ') {y--; } anssteps++; if (!is_inmap (x, y))//is still in the map {printf ("%d step (s) to exit\n", anssteps ); Break } if (Step[x][y]==-1)//step[x][y] equals-1 indicates that the current point is not traversed and replaced by Anssteps s Tep[x][y]=anssteps; else//Otherwise already visited, there is a dead loop, output {printf ("%d step (s) before a loop of%d step (s) \ n ", Step[x][y],anssteps-step[x][y]); Break } } }}
Hdu-1035-robot Motion (open two arrays for a simple simulation, saying that the simulation has been done lately ...) C + +)