POJ 1573 Robot Motion simulation of algorithm problem

Source: Internet
Author: User

A robot has been programmed to follow the instructions into its path. Instructions for the next direction the robot are 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" side of Grid 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 in Grid 2:the robot goes through 3 instructions only once, and then starts a loop through 8 instruct Ions, and never exits.

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

Input

There would be one or more grids for a to navigate. The data for the following form. On the are three integers separated by blanks:the number of rows in the grid, the number of columns in the GRI D, and the number of the column in which the "robot enters from the". The possible entry columns are numbered with one on the left. Then come the direction instructions. Each grid would have at least one and at most rows and columns of instructions. The lines of instructions contain only to the characters N, S, E, or W with no blanks. The end of the input was indicated by a row containing 0 0 0.

Output

For each grid in the input there was one line of output. Either the robot follows a certain number of instructions and exits the grid on any one of the four sides or else the robot F Ollows the instructions on a certain number of locations once, and then to instructions on some number of locations Repea tedly. The sample input below corresponds to the two grids above and illustrates 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 0

Sample Output

Ten step (s) to exit

3 Step (s) before a loop of 8 step (s)

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.