Dungeon Master (BFS与DFS的应用)
个人心得:一开始用DFS弄了半天一直输出不了结果,后面发现并没有进行判断;好不容易能够得出答案,结果超时了,才发现原来要用BFS;
对于DFS:
从一个点开始模拟能走的所有步骤,注意边界条件,走到不能走时返回上一步继续循环;耗时比较大,主要要注意当前的动作
格式的话
void dfs(int step)
{
判断边界
尝试每一种可能
{
注意标记;
继续下一步;
}
返回
}
BFS广度搜索,能够走得位置都走,将能到达的位置进入队列,当一个位置的所有动作完成时出队列,注意标志不改变,当队列为空时搜索完毕,当然找最小步伐时第一次到达时便是最小步骤!
struct Node
{
int x;//横坐标
int y;//纵坐标
int f;//纪录当下的编号,有时输出路径
int sum;//总步数
};
将开始的位置放入队列,sum=0,每一种可能判断,若能走标志并且进入队列,此时sum+1;一个位置的所有动作完成后,队列head出列;若查找到,退出,此时的总步数为queue【tail-1】.sum;
放题
Is an escape possible? If yes, how long will it take?
Input
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct Node
{
int x,y,z;
int sum; };
int L,R,C;
char mapa[][][];
int book[][][];
void bfs(int a,int b,int c)
{
int next[][]={,,,-,,,,,,,-,,,,,,,-};
queue<Node> s;
Node n;
n.x=a,n.y=b,n.z=c,n.sum=;
book[a][b][c]=;
s.push(n);
while(!s.empty())
{ for(int i=;i<;i++)
{
Node m=s.front();
Node kk;
int t1=kk.x=m.x+next[i][];
int t2=kk.y=m.y+next[i][];
int t3=kk.z=m.z+next[i][];
kk.sum=m.sum+;
if(t1<||t2<||t3<||t1>L||t2>R||t3>C)
continue;
if(mapa[t1][t2][t3]=='E')
{
cout<<"Escaped in "<<kk.sum<<" minute(s)."<<endl;
return ; }
if(mapa[t1][t2][t3]=='.'&&book[t1][t2][t3]==)
{ s.push(kk);
book[t1][t2][t3]=; } }
s.pop(); }
cout<<"Trapped!"<<endl;
}
int main()
{ while(cin>>L>>R>>C)
{
int a,b,c;
if(!L&&!R&&!C)
break;
for(int i=;i<=L;i++)
for(int j=;j<=R;j++)
for(int k=;k<=C;k++)
{
cin>>mapa[i][j][k];
if(mapa[i][j][k]=='S') {a=i,b=j,c=k;} }
memset(book,,sizeof(book));
bfs(a,b,c); } return ; }
Dungeon Master (BFS与DFS的应用)的更多相关文章
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- hdu 2251 Dungeon Master bfs
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17555 Accepted: 6835 D ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- poj 2251 Dungeon Master (BFS 三维)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- [poj] Dungeon Master bfs
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- POJ2251 Dungeon Master(bfs)
题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
随机推荐
- 04 Spring框架 依赖注入(一)
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们讲了几个bean的一些属性,用来限制我们实例创建过后的状态. 但是细心的我们会发现其实上面demo创建的实例并 ...
- Apache Kudu
Apache Kudu是由Cloudera开源的存储引擎,可以同时提供低延迟的随机读写和高效的数据分析能力.Kudu支持水平扩展,使用Raft协议进行一致性保证,并且与Cloudera Impala和 ...
- jstl-functions标签
比如需要再jstl中定义一个String类型的数组 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl ...
- Delphi 2005 以上版本GIF动画播放设置
源: Delphi 2005 以上版本GIF动画播放设置
- 20145231《Java程序设计》课程总结
20145231 <Java程序设计>课程总结 每周读书笔记链接汇总 ● 20145231<Java程序设计>第一周学习总结 ●20145231<Java程序设计> ...
- SDWebImage第三方库学习
1.基本使用方法 //异步下载并缓存 - (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT; //使用占位图片,当 ...
- Bootstrap3全局CSS样式
目录 1. 概览 2. 栅栏系统 3. 文本 4. 列表 5. 代码 6. 表格 7. 表单 7.1 基本实例 7.2 内联表单 7.3 水平排列的表单 8. 按钮 9. 图片 10. 辅助类 10. ...
- python安装包
1. python常用包下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy numpy包下载地址:https://pypi.python.org ...
- ubuntu 忘记root密码
Ubuntu14.04系统中,因为误操作导致管理员密码丢失或无效,并且忘记root密码,此时无法进行任何root/sudo权限操作.可以通过GRUB重新设置root密码,并恢复管理员账户到正常状态. ...
- java定时任务Quartz Demo(2.X)
直接上代码 public class HelloQuartz implements Job{ @Override public void execute(JobExecutionContext Jec ...