算法题之POJ 1573 Robot Motion 模拟


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 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 5

NEESWE

WWWESS

SNWWWW

4 5 1

SESWE

EESNW

NWEEN

EWSEN

0 0 0

Sample Output

10 step(s) to exit

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

时间: 2024-09-20 08:03:48

算法题之POJ 1573 Robot Motion 模拟的相关文章

算法题:POJ 1068 Parencodings 栈模拟

Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence). q By an integer

算法题之poj 1961 Period(KMP, 最短循环节)

题目链接: POJ  : http://poj.org/problem?id=1961 HDU : http://acm.hdu.edu.cn/showproblem.php?pid=1358 ZOJ   : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2177 题目大意: 给定一个长度为n的字符串s,求它的每个前缀的最短循环节.换句话说,对于每个i (2<=i<=n),求一个最大的整数K>1(如果K存在),使

算法题:poj 2541 Binary Witch(KMP水过,逆序转换)

链接: http://poj.org/problem?id=2541 分析与总结: 做这题估算了下复杂度,觉得无论KMP再怎么快,这题暴力也肯定要超时的. 想了很久也没想出个好办法,于是决定暴力之,但是TLE了....于是就放了几天.之后看了下discuss ,这题的正解应该是状态压缩dp,不过目前我还不懂,跪了. 之后百度发现也可以用KMP水过,虽然是因为数据水才过的,不过这种思路很巧妙,值得借鉴! 直接暴力是枚举字符串的后面13个的字母,然后再用KMP匹配,这样的话,就绪要枚举多次,分别是

算法题:poj 3080 Blue Jeans(KMP匹配,枚举子串)

链接: http://poj.org/problem?id=3080 题目大意: 给出m(2<=m<=10)个DNA序列, 求出这m个序列中最长的公共字串.如果有多个相同长度的, 输出字典序最小的. 分析与总结: 依次枚举所有的子串, 然后再看是否在所有序列中都能够匹配.保存下长度最大且字典序最小的序 列. 代码: #include<iostream> #include<cstdio> #include<cstring> using namespace st

算法题:poj 2752 Seek the Name, Seek the Fame(理解KMP的失配函数!)

链接: http://poj.org/problem?id=2752 题目大意: 给一个字符串S, 求出所有前缀pre,使得这个前缀也正好是S的后缀. 输出所有前缀的结束位置. 例如 "ababcababababcabab", 以下这些前缀也同时是S的后缀 ab  :  位置2 abab  : 位置4 ababcabab : 位置9 ababcababababcabab : 位置 18 分析与总结: 这题,关键在于对KMP的失配函数的理解.只要真正理解了,那么做出来完全不成问题. 下面

算法题:POJ 2006 Litmus Test (简单数学&amp;amp;弱酸的电离常数)

Litmus Test http://poj.org/problem?id=2006 Time Limit: 1000MS Memory Limit: 30000K Description The pH scale measures the concentration of protons (H+) in a solution and, therefore, its acidity or alkalinity. The pH value of a solution is a number bet

算法题:POJ 2418 Hardwood Species (二叉搜索树)

Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. America's temperate climates produce forests with hundreds of hardwood species -- trees that share certain biological c

编码-poj 2058算法题Word Encoding完整代码

问题描述 poj 2058算法题Word Encoding完整代码 题目描述 In any language, certain combinations of letters do not appear (or at least appear so seldom that they can be considered non-existent). For instance, there are no English words containing the three letter combin

算法题——投篮比赛获胜概率问题

问题描述: 甲乙两人比赛投篮.约定甲先投篮,每人投篮投进一球,则继续投球,若投失一球,则换人投球.初始积分为1分,甲每投进一球,积分加1分:乙每投进1球,积分减1分.若积分达到N分(N>1),甲获胜:若积分减至0分,乙获胜.假定甲投进的概率为P1(0<P1<1),乙投进的概率为P2(0<P2<1).那么这场投篮比赛,甲获胜的概率P为多少?     很显然的,甲获胜的概率P是和P1.P2.N相关的. P1越大,P越大 P2越大,P越小 N越大,P越小   不失一般性,假设P1=