HDU 1035 robot motion (simple question, simulation)

Source: Internet
Author: User
Robot Motion

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3466 accepted submission (s): 1608

Problem description

A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N North (up the page)
S South (down the page)
E East (to the right on the page)
W West (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts South (down ). the path the robot follows is shown. the robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: The robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.

 

Inputthere will be one or more grids for robots to navigate. the data for each is in the following form. on the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. the possible entry columns are numbered starting with one at the left. then come the rows of the direction instructions. each grid will have at least one and at most 10 rows and columns of instructions. the lines of instructions contain only the characters N, S, E, or W with no blanks. the end of input is indicated by a row containing 0 0.

 

Outputfor each grid in the input there is one 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 follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. the sample input below corresponds to the two grids above and below strates the two forms of output. the word "Step" is always immediately followed by "(s)" whether or not the number before it is 1.

 

Sample input3 6 5 neeswe wwwess snwwww 4 5 1 seswe eesnw nween ewsen 0 0

 

Sample output10 step (s) to exit 3 step (s) before a loop of 8 step (s)

 

Sourcemid-Central USA 1999 simple simulation, water question
# Include <stdio. h> # Include <String . H> # Include < String . H> Const   Int Maxn = 1000  ;  Char  Map [maxn] [maxn];  Int  T [maxn] [maxn];  Int  Main (){  Int  N, m, K; Int  X, Y;  While (Scanf ( "  % D  " , & N ,& M )){  If (N = 0 & M = 0 ) Break  ; Scanf (  "  % D  " ,&K );  For ( Int I = 0 ; I <n; I ++) scanf ( "  % S  " ,& Map [I]); x = 0  ; Y = K- 1  ; Memset (t,  0 , Sizeof  (T); t [x] [Y] = 1  ;  While ( 1  ){  If (Map [x] [Y] = '  W  '  ) {Y -- ;  If (Y < 0  ) {Printf ( "  % D step (s) to exit \ n  " , T [x] [Y + 1  ]);  Break  ;}  If (T [x] [Y]! = 0  ) {Printf (  "  % D step (s) before a loop of % d step (s) \ n  " , T [x] [Y]- 1 , T [x] [Y +1 ]-T [x] [Y] + 1  );  Break  ;} T [x] [Y] = T [x] [Y + 1 ] + 1  ;}  Else   If (Map [x] [Y] = '  E  '  ) {Y ++;  If (Y> = M) {printf (  "  % D step (s) to exit \ n  " , T [x] [y- 1  ]);  Break  ;}  If (T [x] [Y]! = 0  ) {Printf (  " % D step (s) before a loop of % d step (s) \ n  " , T [x] [Y]- 1 , T [x] [y- 1 ]-T [x] [Y] + 1  );  Break  ;} T [x] [Y] = T [x] [y- 1 ] + 1  ;}  Else   If (Map [x] [Y] ='  N  '  ) {X -- ;  If (X < 0  ) {Printf (  "  % D step (s) to exit \ n  " , T [x + 1  ] [Y]);  Break ;}  If (T [x] [Y]! = 0  ) {Printf (  "  % D step (s) before a loop of % d step (s) \ n  " , T [x] [Y]- 1 , T [x + 1 ] [Y]-T [x] [Y] + 1  );  Break  ;} T [x] [Y] = T [x +1 ] [Y] + 1  ;}  Else  {X ++ ;  If (X> = N) {printf (  "  % D step (s) to exit \ n  " , T [X- 1  ] [Y]);  Break ;}  If (T [x] [Y]! = 0  ) {Printf (  "  % D step (s) before a loop of % d step (s) \ n  " , T [x] [Y]- 1 , T [X- 1 ] [Y]-T [x] [Y] + 1  );  Break  ;} T [x] [Y] = T [X-1 ] [Y] + 1  ;}}}  Return   0  ;} 

 

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.