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.
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 0.
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 illustrates the two forms of output. The word “step” is always immediately followed by “(s)” whether or not the number before it is 1.
题意:
机器人在地图的上边界,根据当前字母进行移动。 当机器人走出地图,输出 "机器人走的步数" step(s) to exit 当机器人进入循环,输出 "机器人进入循环前走的步数" step(s) before a loop of "循环长度" step(s)思路:
地图:地图可以存于一个二维数组map(从(1,1)开始),同时用一个同规模的数组book(初始值为0),记录是否走过、走过对应位置时为第几步。状态:机器人从初始位置开始识别当前位置的字母,进行移动。每次移动更新机器人的位置信息,记录当前所走的步数,判断是否走出地图,或者进入循环。结果:若走出地图,则当前所走步数即为要输出的数字;若进入循环,则循环的长度为,当前所走步数减去目前所在位置在book中的数值,而进入循环前所走步数为当前位置在book中的数值。