(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 ...
随机推荐
- altium designer 13 学习之添加汉字
在altium desginer中如果你是想添加英文还是比较方便的,基本直接就可以输入了,但是添加中文就不是那么简单了,下面不介绍下如何在altium designer中快速的添加自己想要的中文 工具 ...
- SQL 2008 R2 启动失败 提示 请求失败或服务未及时响应
为什么启动sql server 配置管理器出现请求失败或服务未及时响应_百度知道 http://zhidao.baidu.com/link?url=ElemzIan6I2CqJsd7-7uk5TV25 ...
- Spring中的Resource
Spring中的资源定义:Resource此接口的全名为:org.springframework.core.io.Resource比较常用的资源定义的实现类为:1.ClassPathResource ...
- Creating a web server in pure C(c/c++ 写web server)
一个简单的例子:https://github.com/fekberg/GoHttp 一个运行在windows上例子:http://www.adp-gmbh.ch/win/misc/webserver. ...
- bugumongo--ConnectToMongoDB
连接MongoDB 在能够对MongDB进行操作之前,需要使用BuguConnection连接到MongoDB数据库.代码如下: BuguConnection conn = BuguConnectio ...
- 将OutLook.exe注册为服务,让其一直保持开启状态
类似于TaobaoProtect.exe是由TBSecSvc服务启动的 http://stackoverflow.com/questions/3582108/create-windows-servic ...
- hdu 4502 吉哥系列故事——临时工计划(dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4502 思路:一个简单的dp ,比赛时没做出来呢..... d[i]代表 到第i天时的最大值 #includ ...
- Office启动加载vs。。。项
PowerPoint: 选项->加载项->Chinese Translation Addin->管理[COM加载项]转到->取消Chinese Translation Addi ...
- win8.1 64 安装用友T3+sql2005-64步骤
1. 环境:win8.1 64 专业版 4G内存 .net framwork 3.5 2.初始过程及所需软件 安装sql2008数据库,安装完T3发现并不支持此数据库,运行T3老是出现连接数据时的 ...
- 【原创】 Shuffling
在机器学习领域中,经常会听到“shuffling"这个术语.那么,shuffling到底是什么意思呢. 通常,shuffling指的是在SGD怎样依赖训练数据输入顺序的算法中,将训练数据随机 ...