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. MSDN 同步部分 个人笔记

    (在知乎看到轮子哥说,掌握了MSDN上的并发部分 和 线程与进程部分就可以掌握所有语言的多线程编程,我在网上翻了一下并没有中文版,所以决定自己翻译一下...) 目录: 线程之间协同运行的方式有许多种, ...

  2. TCP/IP学习笔记(4)------ICMP,ping,traceroute

    IMCP协议介绍 当传送IP数据包发生错误--比如主机不可达,路由不可达等等,ICMP协议将会把错误信息封包,然后传送回给主机.给主机一个处理错误的机会,这 也就是为什么说建立在IP层以上的协议是可能 ...

  3. 2017-10-03-morning

    #include <algorithm> #include <cstring> #include <cstdio> inline void read(int &am ...

  4. pageContext 获取Session 为null原因

    问题:在J2EE应用中.发如今自己定义标签中取不到session: HttpSession session = pageContext.getSession(); 你会发现session的值可能是空的 ...

  5. 消息驱动bean(MDB)实例

    到眼下为止前面介绍的有关JavaEE的东西都是同步的.也就是说调用者调用某个方法.那么这种方法必须马上运行并返回运行结果. 用官方一些的语言来说就是"client通过业务接口调用一个方法,在 ...

  6. dorker 安装

    http://www.docker.org.cn/book/install/install-docker-win7-win8-requirements-38.html1. 你先去下载Docker To ...

  7. centOS的联网问题

    centOS连接了一个下午,没连上网络,隔了两天,又试了一下午才把网连上,一直查centOS的网络连接问题都搞不定,最后还是问了朋友怎么给虚拟机联网谈到虚拟网卡的问题.建议可以看看网络适配器,VMwa ...

  8. 通过spark rdd 求取 特征的稀疏向量

    通过spark rdd 求取  特征的稀疏向量 spark 类标签的稀疏 特征向量 - bonelee - 博客园 http://www.cnblogs.com/bonelee/p/7814081.h ...

  9. Open Source Computer Vision Library

    https://opencv.org/ OpenCV (Open Source Computer Vision Library) is released under a BSD license and ...

  10. windows console Kill PID 端口查看

    开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...