Poj 1573 robot motion (Water Simulation)

Source: Internet
Author: User
Robot Motion
Time limit:1000 ms   Memory limit:10000 K
Total submissions:7892   Accepted:3812

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.

Input

There 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.

Output

For 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 Input

 
3 6 5neeswewwwesssnwwww4 5 1sesweeesnwnweenewsen0 0 0

Sample output

 
10 step (s) to exit3 step (s) before a loop of 8 step (s)

Source

Mid-Central USA 1999 does not need to be explained .. Very watery simulation ..
# Include <stdio. h> # Include < String . H> Const   Int Maxn = 20  ;  Char  Map [maxn] [maxn];  Int  A [maxn] [maxn];  Int  N, m;  Void Solve ( Int  S) {memset (,  0 ,Sizeof  (A); [  0 ] [S- 1 ] = 1  ;  Int X = 0  ;  Int Y = s- 1  ;  While ( 1  ){  Int Tx = x, Ty =Y;  If (Map [x] [Y] = '  N  ' ) TX -- ;  Else   If (Map [x] [Y] = '  S  ' ) TX ++ ;  Else   If (Map [x] [Y] = '  W ' ) Ty -- ;  Else   If (Map [x] [Y] = '  E  ' ) Ty ++ ;  If (TX < 0 | TX> = n | ty < 0 | Ty> = M) {printf (  "  % D step (s) to exit \ n  " , A [x] [Y]);  Return  ;}  Else   If (A [TX] [ty]! = 0  ) {Printf (  "  % D step (s) before a loop of % d step (s) \ n  " , A [TX] [ty]- 1 , A [x] [Y]-A [TX] [ty] + 1  );  Return ;}  Else  {A [TX] [ty] = A [x] [Y] + 1  ; X = TX; y = Ty ;}}}  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int S;  While (Scanf ( "  % D  " , & N, & M, & S )! = EOF ){  If (N = 0 & M = 0 & Amp; S = 0 ) Break  ;  For ( Int I =0 ; I <n; I ++ ) Scanf (  "  % S  " ,& Map [I]); solve (s );}  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.