HDU1035: Robot Motion

Source: Internet
Author: User

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.

 


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 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0


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

 

Simple simulation. WA once failed to initialize map.

 

[Cpp] # include <stdio. h>
# Include <string. h>
 
Char map [20] [20];
Int mat [20] [20];
 
Int main ()
{
Int n, m, t, I, j, step;
While (~ Scanf ("% d", & n, & m, & t ))
{
Int flag = 0;
Memset (mat, 0, sizeof (mat ));
Memset (map, '\ 0', sizeof (map ));
If (! N &&! M)
Break;
For (I = 0; I <n; I ++)
{
Scanf ("% s", map [I]);
}
Step = 1;
Int x, y, loop = 0;
X = 0;
Y = T-1;
While (1)
{
If (map [x] [y] = 'n '&&! Mat [x] [y])
{
Mat [x] [y] = step;
X --;
}
Else if (map [x] [y] ='s '&&! Mat [x] [y])
{
Mat [x] [y] = step;
X ++;
}
Else if (map [x] [y] = 'E '&&! Mat [x] [y])
{
Mat [x] [y] = step;
Y ++;
}
Else if (map [x] [y] = 'W '&&! Mat [x] [y])
{
Mat [x] [y] = step;
Y --;
}
Else if (mat [x] [y]) // when it reaches the repetition point, the number of steps must be-1.
{
Step --;
Loop = step-mat [x] [y] + 1;
Flag = 1;
Break;
}
Else if (x <0 | y <0 | x> = n | y> = m) // when it reaches this point, it is out of bounds, so the number of steps must be-1
{
Step --;
Break;
}
Step ++;
}
If (flag)
Printf ("% d step (s) before a loop of % d step (s) \ n", step-loop, loop );
Else
Printf ("% d step (s) to exit \ n", step );
}
 
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.