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 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)
解题思路:简单地搜索一遍即可,水过!
AC代码:
 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
int row,col,st,step,cnt[maxn][maxn];char s[maxn][maxn];
void dfs(int x,int y){
while(true){//遍历一次即可
if(x<||x>=row||y<||y>=col){cout<<step<<" step(s) to exit"<<endl;return;}
if(cnt[x][y]>-){cout<<cnt[x][y]<<" step(s) before a loop of "<<(step-cnt[x][y])<<" step(s)"<<endl;return;}
//只要cnt[x][y]>-1,说明该点已经遍历过了,直接退出
cnt[x][y]=step++;
if(s[x][y]=='E')y++;
else if(s[x][y]=='W')y--;
else if(s[x][y]=='N')x--;
else x++;
}
}
int main(){
while(~scanf("%d%d%d",&row,&col,&st)&&(row+col+st)){
memset(cnt,-,sizeof(cnt));//全部初始化为-1
for(int i=;i<row;++i){
getchar();
for(int j=;j<col;++j)
scanf("%c",&s[i][j]);
}
step=;//初始值为0
dfs(,st-);
}
return ;
}

题解报告:hdu 1035 Robot Motion(简单搜索一遍)的更多相关文章

  1. HDOJ(HDU).1035 Robot Motion (DFS)

    HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...

  2. (step 4.3.5)hdu 1035(Robot Motion——DFS)

    题目大意:输入三个整数n,m,k,分别表示在接下来有一个n行m列的地图.一个机器人从第一行的第k列进入.问机器人经过多少步才能出来.如果出现了循环 则输出循环的步数 解题思路:DFS 代码如下(有详细 ...

  3. [ACM] hdu 1035 Robot Motion (模拟或DFS)

    Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...

  4. hdu 1035 Robot Motion(dfs)

    虽然做出来了,还是很失望的!!! 加油!!!还是慢慢来吧!!! >>>>>>>>>>>>>>>>> ...

  5. HDU 1035 Robot Motion(dfs + 模拟)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035 这道题比较简单,但自己一直被卡,原因就是在读入mp这张字符图的时候用了scanf被卡. ...

  6. hdu 1035 Robot Motion(模拟)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  7. hdoj 1035 Robot Motion

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. DFS与BFS题解:[kaungbin]带你飞 简单搜索 解题报告

    DFS and  BFS 在解题前我们还是大致讲一下dfs与bfs的.(我感觉我不会bfs) 1.DFS dfs(深度优先算法) 正如其名,dfs是相当的深度,不走到最深处绝不回头的那种. 深度优先搜 ...

  9. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

随机推荐

  1. 开车旅行(codevs 1199)

    题目描述 Description 小A 和小B决定利用假期外出旅行,他们将想去的城市从1到N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i的海拔高度为Hi, ...

  2. 用jQuery向div中添加Html文本内容

    前台代码: <link href="http://www.cnblogs.com/Content/themes/base/jquery-ui.css" rel="s ...

  3. Uva - 12230 Crossing Rivers (数学期望)

    你住在村庄A,每天需要过很多条河到另一个村庄B上班,B在A的右边,所有的河都在A,B之间,幸运的是每条船上都有自由移动的自动船, 因此只要到达河左岸然后等船过来,在右岸下船,上船之后船的速度不变.现在 ...

  4. 断路器监控(Hystrix Dashboard)

    继上一篇http://www.cnblogs.com/EasonJim/p/7613595.html介绍了断路器之后,其实它还提供了一个管理页面来监控这些应用的调用数据. 首先,我是基于上一个例子Zo ...

  5. DTRACE简介(1)

    https://blogs.oracle.com/swan/entry/dtrace%E7%AE%80%E4%BB%8B By samwan on 三月 20, 2007 记得几年前看过一部美国大片叫 ...

  6. Eclipse 搭建tomcat+动态项目完整版

    1. Tomcat搭建 1.新加服务器,右击控制台的server目录->new->server->选择本地tomcat 2.配置tomcat属性(如果更改失败,将tomcat下的项目 ...

  7. UVa 340 Master-Mind Hints(猜数字游戏的提示)

    题意  猜数字游戏  统计猜的数字有多少个数字位置正确  有多少个数字在答案中出现可是位置不对  每一个字符仅仅能匹配一次 直接匹配每位数 #include<cstdio> #includ ...

  8. poj 2318 TOYS &amp; poj 2398 Toy Storage (叉积)

    链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...

  9. storm ——Understanding the Parallelism of a Storm Topology

    http://www.michael-noll.com/blog/2012/10/16/understanding-the-parallelism-of-a-storm-topology/ 这篇文章好 ...

  10. 工作总结 default Console.WriteLine(default(Guid));

    泛型代码中的默认关键字 在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T: T 是引用类型还是值类型. 如果 T 为值类型,则它是数值还是结构. 给定参数化 ...