轻量级前端框架助力开发者提升项目效率与性能
623
2022-08-26
POJ 1573 Robot Motion (模拟)
Description
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 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 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 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0 0
Sample Output
10 step(s) to exit3 step(s) before a loop of 8 step(s)
题意
给出一个迷宫与人物的起始位置,人物沿地图的标记行走,输出他在第多少步时走出迷宫或者在第多少步时陷入循环以及循环的长度。
思路
迷宫可以用二维数组来存储,每一个点存储的是它的移动方向。
然后从初始位置模拟行走,如果某一次脱离地图边缘,即成功离开;
走过的点都标记为第一次走到这里所需要的步数,若在行走过程中遇到已经走过的点,则说明当前陷入循环,循环的长度即两个步数之差。
AC 代码
#include
发表评论
暂时没有评论,来抢沙发吧~