【题目描述】

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 lineTrapped!

Sample Input

3 4 5
S....
.###.
.##..
###.######
#####
##.##
##...#####
#####
#.###
####E1 3 3
S##
#E#
###0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

【题目分析】
一道很简单的三维BFS,但是因为我刚开始的时候没有在入队的时候就设置该点已经入队导致疯狂入队然后一直爆空间,还完全找不到问题
代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;const int MAXN=35;
int a[MAXN][MAXN][MAXN];
char s[MAXN];
int L,R,C,u,v,w,x,y,z;
struct node
{int x,y,z;int step;
};
node S,E,p,t;const int drc[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int ans;void BFS()
{queue<node> q;q.push(S);while(!q.empty()){p=q.front(); q.pop();if(p.x==E.x && p.y==E.y && p.z==E.z){ans=p.step;while(!q.empty()) q.pop();return;}x=p.x; y=p.y; z=p.z;//a[z][x][y]=0; //就因为这里一直错for(int i=0;i<6;i++){u=x+drc[i][0]; v=y+drc[i][1]; w=z+drc[i][2];if(u<0 || u>=R || v<0 || v>=C || w<0 || w>=L) continue;if(a[w][u][v]==0) continue;t.x=u; t.y=v; t.z=w; t.step=p.step+1;a[w][u][v]=0;    //很重要q.push(t);}}
}int main()
{while(~scanf("%d%d%d",&L,&R,&C)){if(L==0 && R==0 && C==0) break;memset(a,0,sizeof(a));ans=0;for(int i=0;i<L;i++){for(int j=0;j<R;j++){scanf("%s",s);for(int k=0;k<C;k++){if(s[k]=='.') a[i][j][k]=1;else if(s[k]=='S'){S.z=i; S.x=j; S.y=k; S.step=0;a[i][j][k]=1;}else if(s[k]=='E'){E.z=i; E.x=j; E.y=k; E.step=0;a[i][j][k]=1;}}}}BFS();if(ans==0){printf("Trapped!\n");}else{printf("Escaped in %d minute(s).\n",ans);}}return 0;
}

Dungeon Master——BFS相关推荐

  1. POJ-2251 Dungeon Master bfs搜索

    就是将普通的二维推广到三维,方向变成了六个. 代码如下: #include <cstring> #include <cstdio> #include <cstdlib&g ...

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

    三维空间里BFS最短路 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #incl ...

  3. POJ 2251 Dungeon Master(三维BFS求最短路径)

    3D dungeon 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 [提交][状态][讨论版][命题人:201506020829][Edit] [TestData] 题 ...

  4. Dungeon Master 地下城大师(BFS进阶)

    题目链接:2251 -- Dungeon Master 知道你看不懂题(手动滑稽):友情链接. 题意:找到从S到E的最少步数的路径,输出该步数,不过有意思的是这个类似迷宫问题不是二维的,是一个三维迷宫 ...

  5. Dungeon Master(地牢大师、底下城主)三维bfs

    1253:Dungeon Master(地牢大师.底下城主) 要敢于变通 和普通bfs的区别,仅仅在于数组开三维+搜索方向多了上下(现在的搜索方向是 上下东南西北) bfs多组输入要记得清空队列 // ...

  6. Dungeon Master题解bfs

    Dungeon Master 题面翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 题解思路及TIME EXCEEDED问题 题解 超时题解(个人觉得没问题) 题面翻译 ...

  7. Dungeon Master(poj2251,bfs)

    http://poj.org/problem?id=2251 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15203 Dun ...

  8. BFS - Dungeon Master

    Dungeon Master 题目链接: https://vjudge.net/problem/POJ-2251 题目: You are trapped in a 3D dungeon and nee ...

  9. (POJ - 2251)Dungeon Master(bfs)

    题目链接:2251 -- Dungeon Master (poj.org) 这是一个典型的bfs迷宫问题,只不过是三维的,唯一需要注意的就是输入要用cin,不要用scanf,因为有换行,其他没什么了, ...

最新文章

  1. scala可变长度参数函数
  2. 【转载】视频CDN技术原理与流程说明
  3. 阮一峰react demo代码研究的学习笔记 - how is h1 got parsed - not answer
  4. vscode设置templates_在VScode中创建你的代码模板的方法
  5. vue.js java php_听说Java程序员喜欢AngularJS,PHP程序员喜欢Vue.js
  6. Android程序设计报告总结,Android编程常用技巧实例总结
  7. 李瑾博士:信誉的建立是否“不计成本”?
  8. ubuntu下配置nginx支持phpt(unix:/var/run/php5-fpm.sock failed (2: No such file or directory) )
  9. C++中typedef用法说明
  10. 单片机实验报告太原理工大学_太原理工大学单片机实验报告
  11. cad解除块的快捷命令_CAD撤销上一步和恢復下一步的快捷键是什么?
  12. 【转】基于gamebryo引擎开发过程中组件的应用和取舍 By 宋晓宇
  13. 经典排序算法之:堆排序
  14. TREG(Transformed Regression for Accurate Tracking)
  15. Arcgis连接sql server发布地图服务详解
  16. 计算机网络基础——网络的性能
  17. 深圳租房数据可视化分析【Plotly库绘图】
  18. 软件体系结构与设计模式——课程总体介绍(01-03)
  19. 利用pymupdf编辑修改pdf
  20. P85.2.(2)回文是指正读反读均相同的字符序列,如“abba”和“abdba”均是回文,但“good”不是回文。试写一个算法判定给定的字符序列是否是回文。(提示:将一半的字符入栈)(C语言描述)

热门文章

  1. android-线程池-最顺手的写法
  2. Java并发编程笔记之ConcurrentLinkedQueue源码探究
  3. 【原创】MapReduce编程系列之表连接
  4. windows 7资源管理器崩溃解决方法
  5. 应用ADO.net得到表
  6. python爬取会议论文pdf_【python2.7】爬取知网论文
  7. 声速的测量的实验原理和应用_声速的测定实验报告心得体会
  8. Flask和mysql多线程_Flask解析(二):Flask-Sqlalchemy与多线程、多进程
  9. mysql触发器区分新增 修改_MySQL触发器 , 判断更新操作前后数据是否改变
  10. qt结构体嵌套结构体方法_9.2 C++结构体类型变量