个人心得:一开始用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;

放题

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
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

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

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的应用)的更多相关文章

  1. Dungeon Master poj 2251 dfs

    Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855 ...

  2. hdu 2251 Dungeon Master bfs

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 D ...

  3. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  4. Dungeon Master bfs

    time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...

  5. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  6. 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 ...

  7. [poj] Dungeon Master bfs

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  8. poj 2251 Dungeon Master( bfs )

    题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A,     上代码 #include <iostream> #include<cst ...

  9. POJ2251 Dungeon Master(bfs)

    题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...

  10. POJ 2251 Dungeon Master bfs 难度:0

    http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...

随机推荐

  1. 静态库引入引起的错误解决方案,ld: warning: ignoring file ”…/XXX.a”, file was built for archive which is not the architecture being linked (armv7): “…/XXX.a” Undefined symbols for architecture armv7: "_OBJC_CLASS_$

    想目中不免会引入一些静态库,可是有时加入'.a'文件后编译便会报以下错误 ld: warning: ignoring file ”…/XXX.a”, file was built for archiv ...

  2. springboot——数据层访问搭建 集成Duid连接池

    springboot中默认是使用的tomcat的连接池,如果我们想要第三方的连接池,我们这么配置呢? 首先在application.yml文件中注释掉之前数据库的配置,重新用druid的方式配置: # ...

  3. verilog FAQ(zz)

    1. What is the race condition in verilog? Ans :The situation when two expressions are allowed to exe ...

  4. R的基础学习之数据结构

    来源:http://blog.qiubio.com:8080/archives/3753/4 1.atomic vector :一维的,放置同一类型数据的数据类型 1.1创建:由c()函数 ,seq( ...

  5. Sublime text3装入插件Anaconda

    好多人在学习编写代码的时候,会有不同的编辑器,大神们建议我使sublime,但是我在里面写代码,没有一点提示,我的天,不得烧坏我的脑子啊.学Python我用的Ananconda,但是这个spyder有 ...

  6. Grafana连接Prometheus监控Docker平台

    Grafana是一款开源的分析平台. Grafana allows you to query, visualize, alert on and understand your metrics no m ...

  7. Xshell 5 上传下载插件

    #yum -y install lrzsz #rz 上传 sz用法: 下载一个文件 sz filename 下载多个文件 sz filename1 filename2 下载dir目录下的所有文件,不包 ...

  8. springmvc文件上传的基本描述

    SpringMVC的文件上传,底层也是使用的Apache的Commons-fileupload 可以分为三步: 1.导入依赖包 <!-- 文件上传的依赖 --> <dependenc ...

  9. web容器调用Filter和Servlet顺序学习

    web容器调用Filter和Servlet顺序学习    一直对Filter和Servlet在哪里被web容器调用迷惑,后查看tomcat源码,揭开了其面纱.1. 下面是一个简单的时序图: 2. 对上 ...

  10. ubuntu上安装nodejs

    目录: 1. nodejs的下载 2. 解压和安装 3. 安装过程中出现过的问题 4. 总结 1. nodejs的下载 我刚开始没有linux系统,于是安装了nodejs的windows版本进行学习. ...