(step 4.3.5)hdu 1035(Robot Motion——DFS)
题目大意:输入三个整数n,m,k,分别表示在接下来有一个n行m列的地图。一个机器人从第一行的第k列进入。问机器人经过多少步才能出来。如果出现了循环
则输出循环的步数
解题思路:DFS
代码如下(有详细的解释):
/*
* 1035_1.cpp
*
* Created on: 2013年8月17日
* Author: Administrator
*/
/*简单搜索题,看注释:
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std; /**
* map[][] :用来标记地图
* num[x][y] : 用来标记走到(x,y)这个点的时候用了多少步.
* 如果num[x][y] == 0,则证明这一步还没走过。否则,根据题意则可能出现了循环
*
* n: 行数
* m: 列数
* k: 从第一行的第k列进入地图
* t: 总共走了多少步
* lop: 循环的步数
*/
char map[11][11];
int num[11][11];
int n, m, k, lop, t; /**
* 判断是否越界
*/
bool Overmap(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m) {
return true;
} else {
return false;
}
} void Dfs(int x, int y) {
//如果越界了或者是出现了循环
if (Overmap(x, y) || lop > 0) {
if (lop > 0) {
printf("%d step(s) before a loop of %d step(s)\n", t - lop, lop);
} else {
printf("%d step(s) to exit\n", t);
}
return ;
} if (num[x][y] == 0) {//如果这一步还没有走过
num[x][y] = ++t;
} else {//判断是否有循环(如果这一步已经走过,则计算循环的步数)
lop = t - num[x][y] + 1;
}//else //遍历状态
switch(map[x][y]) {
case 'N': x--; Dfs(x, y); x++; break; //Dfs最主要:前加的条件,在之后要返回
case 'E': y++; Dfs(x, y); y--; break;
case 'S': x++; Dfs(x, y); x--; break;
case 'W': y--; Dfs(x, y); y++; break;
}//switch
}//dfs int main() {
while (scanf("%d %d %d", &n, &m, &k) != EOF, n) {
memset(map, 0, sizeof(map));
memset(num, 0, sizeof(num)); lop = 0;
t = 0;
getchar();
for (int i = 1; i <= n; i++) {
scanf("%s", map[i] + 1);
getchar();
}
Dfs(1, k); }
}
(step 4.3.5)hdu 1035(Robot Motion——DFS)的更多相关文章
- HDOJ(HDU).1035 Robot Motion (DFS)
HDOJ(HDU).1035 Robot Motion [从零开始DFS(4)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DF ...
- hdu 1035 Robot Motion(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 + 模拟)
嗯... 题目链接: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 ...
- 题解报告: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 ...
- hdu1035 Robot Motion (DFS)
Robot Motion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
随机推荐
- android 安全需要关注
1.通过签名校验保护,能有效避免应用被二次打包,杜绝盗版应用的产生2.对内存数据进行变换处理和动态跟踪,有效防止数据被获取和修改3.对代码进行加密压缩,可防止破解者还原真实代码逻辑,避免被复制4.多重 ...
- socket编程中write、read和send、recv之间的区别
http://blog.csdn.net/petershina/article/details/7946615 一旦,我们建立好了tcp连接之后,我们就可以把得到的fd当作文件描述符来使用. 由此网络 ...
- module_init宏解析 linux驱动的入口函数module_init的加载和释放
linux驱动的入口函数module_init的加载和释放 http://blog.csdn.net/zhandoushi1982/article/details/4927579 void free_ ...
- [Unity菜鸟] 术语
HUD Mozilla Mozilla基金会,简称Mozilla(缩写MF或MoFo),是为支持和领导开源的Mozilla项目而设立的一个非营利组织. 称作Mozilla公司的子公司,雇佣了一些Mo ...
- 用QT创建新风格: QStyle
转贴: http://hi.baidu.com/yjj2008/blog/item/6cd4a1892ef0d4b60f2444a5.html 本文介绍了如何使用qt提供的接口来设计自己的GUI风格( ...
- 新的HTTP框架:Daraja Framework
https://www.habarisoft.com/daraja_framework.html
- Civil3D二次开发 启动Civil3D异常
用Com方式启动Civil3D时,经常会在第一次启动时出现各种异常. 1. RPC_E_CALL_REJECTED 0x80010001 被呼叫方拒绝接收呼叫 解决方案:外部程序通过COM启动Auto ...
- nigix以及相关
nginx+php的配置 php与nginx整合 http://www.thinkphp.cn/topic/13082.html [入门篇]Nginx + FastCGI 程序(C/C++) 搭建高性 ...
- Factorial
Factorial 计算阶乘 In mathematics, the factorial of a non-negative integer n, denoted by n!, is the pro ...
- OAF与Windows 7版本不兼容黑屏卡顿问题
OAF版本比较原始,在Window7中无法应用配色方案,导致黑屏卡顿问题.(在启动OC4J后,Window7的配色方案还是会还原至原始状态) 修改$JDEV_HOME/jdev/bin/jdev.co ...