Robot MotionTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7372 Accepted Submission (s): 3389
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 has carefully selected several similar problems for you:1312 2181 1501 2614 1426 AC code
#include <stdio.h> #include <string.h>char map[1010][1010];int n,m,x,y;int vis[1010][1010],step[1010][ 1010];void Fun () {if (map[x][y]== ' N ') x--;elseif (map[x][y]== ' E ') y++;else if (map[x][y]== ' S ') x + +; else if (map[x][y]== ' W ') y--;} int main () {while (scanf ("%d%d", &n,&m)!=eof,n| | m) {int i,j;scanf ("%d", &y), memset (map,0,sizeof (map)), for (i=1;i<=n;i++) {scanf ("%s", map[i]+1);} int Px,py,sx,sy;x=1;sx=x;sy=y;memset (vis,0,sizeof (Vis)); Memset (step,0,sizeof (step)); step[x][y]=1;vis[x][y]=1; while (1) {px=x;py=y;fun ();//step[x][y]=step[px][py]+1;if (Vis[x][y]) {printf ("%d step (s) before a loop of%d step (s) \ n", STEP[X][Y]-1,STEP[PX][PY]-STEP[X][Y]+1); break;} Else{if (x<1| | x>n| | y<1| | Y>m) {printf ("%d step (s) to exit\n", step[px][py]); else{vis[x][y]=1;step[x][y]=step[px][py]+1;}}}}
Hdoj topic 1035 Robot Motion (analog)