题解报告:hdu 1035 Robot Motion(简单搜索一遍)
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
Output
Sample Input
Sample Output
#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(简单搜索一遍)的更多相关文章
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- (step 4.3.5)hdu 1035(Robot Motion——DFS)
题目大意:输入三个整数n,m,k,分别表示在接下来有一个n行m列的地图.一个机器人从第一行的第k列进入.问机器人经过多少步才能出来.如果出现了循环 则输出循环的步数 解题思路:DFS 代码如下(有详细 ...
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
Robot Motion Problem Description A robot has been programmed to follow the instructions in its path. ...
- hdu 1035 Robot Motion(dfs)
虽然做出来了,还是很失望的!!! 加油!!!还是慢慢来吧!!! >>>>>>>>>>>>>>>>> ...
- HDU 1035 Robot Motion(dfs + 模拟)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1035 这道题比较简单,但自己一直被卡,原因就是在读入mp这张字符图的时候用了scanf被卡. ...
- hdu 1035 Robot Motion(模拟)
Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...
- hdoj 1035 Robot Motion
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- DFS与BFS题解:[kaungbin]带你飞 简单搜索 解题报告
DFS and BFS 在解题前我们还是大致讲一下dfs与bfs的.(我感觉我不会bfs) 1.DFS dfs(深度优先算法) 正如其名,dfs是相当的深度,不走到最深处绝不回头的那种. 深度优先搜 ...
- POJ1573(Robot Motion)--简单模拟+简单dfs
题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...
随机推荐
- 【重要】MySQL常见面试题
1. 主键 超键 候选键 外键 主 键: 数据库表中对储存数据对象予以唯一和完整标识的数据列或属性的组合.一个数据列只能有一个主键,且主键的取值不能缺失,即不能为空值(Null). 超 键: 在关系中 ...
- Linux下汇编语言学习笔记32 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- [bzoj2561]最小生成树_网络流_最小割_最小生成树
最小生成树 bzoj-2561 题目大意:题目链接. 注释:略. 想法: 我们发现: 如果一条权值为$L$的边想加入到最小生成树上的话,需要满足一下条件. 就是求出原图的最小生成树之后,这个边当做非树 ...
- Java :面向对象
Java :面向对象 直面Java 第001期 什么是面向过程 面向过程是以函数为中心,要解决一个问题,需要把问题分解为一个个的步骤,然后定义一系列的流程,用特定的输入经过函数的处理,最终输出特定的结 ...
- 笔记:Javac编译器
Javac编译器是把 *.java 文件转换为 *.class 文件,是一个前端编译器:对应着有一种把字节码转变为机器码的编译器,称为JIT编译器(Just In Time Compiler),比如 ...
- 洛谷 P3137 [USACO16FEB]圆形谷仓Circular Barn_Silver
P3137 [USACO16FEB]圆形谷仓Circular Barn_Silver 题目描述 Being a fan of contemporary architecture, Farmer Joh ...
- firewalld filter
实现 firewalld 的filter 功能 1. 关闭 INPUT ,关闭OUTPUT (设置黑名单) 任何主机 都 ping 不通 本主机 1>命令 : iptables -P INP ...
- 工作总结 2018-4-13 bootstrapTable 属性 queryParams: queryParams,//参数 get 中 %5B%5D 数组的意思
<table id="dataTable" data-toggle="table" data-show-columns="true" ...
- ubuntu14.04 的ibus不能卸载(安装fcitx输入法框架时可能有这个需求)。出现无system setting有用程序
每年的ubuntu新版本号公布,都会吸引一大批热血青年. 关注越多也让ubuntu越来越好了. 使用ubuntu的人都会在安装系统之后马上安装顺手的输入法,也可能不会.看人. 安装输入法,对于中文输入 ...
- web前端和后端的区别
一句话,展示ui相关的就是前端,否则就是后端. 前端语言:javascript.css和html. 后端就是一些服务.