POJ - 2251 Dungeon Master (搜索)
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<queue>
- #include<cstdio>
- using namespace std;
- const int N=;
- int h,m,n,s,x,y,z;
- char a[N][N][N];
- int dl[][]={{,,-},{,,},{,-,},{,,},{,,},{-,,}};
- struct loc
- {
- int x,y,z,cnt;
- loc(int x,int y,int z,int cnt):x(x),y(y),z(z),cnt(cnt){}
- };
- int pd(int x,int y,int z)
- {
- if(x<||x>h||y<||y>m||z<||z>n) return ;
- else if(a[x][y][z]=='#')return ;
- else if(a[x][y][z]=='E') return -;
- else if(a[x][y][z]=='.') return ;
- else return ;
- }
- int main()
- {
- queue<loc>dem;
- while(cin>>h>>m>>n){
- if(h==)break;
- for(int i=;i<h;i++)
- for(int j=;j<m;j++)
- for(int k=;k<n;k++)
- {
- cin>>a[i][j][k];
- if(a[i][j][k]=='S')x=i,y=j,z=k;
- }
- while(!dem.empty())dem.pop();
- loc tem(x,y,z,);
- dem.push(tem);
- while(!dem.empty())
- {
- tem=dem.front();
- a[tem.x][tem.y][tem.z]='#';
- dem.pop();
- for(int i=;i<;i++)
- {
- if(pd(tem.x+dl[i][],tem.y+dl[i][],tem.z+dl[i][])==-)
- {
- loc t(tem.x+dl[i][],tem.y+dl[i][],tem.z+dl[i][],tem.cnt+);
- tem=t;
- goto en;
- //cout<<tem.x+dl[i][0]<<' '<<tem.y+dl[i][1]<<' '<<tem.z+dl[i][2]<<endl;
- }
- else if(pd(tem.x+dl[i][],tem.y+dl[i][],tem.z+dl[i][])==)
- {
- loc t(tem.x+dl[i][],tem.y+dl[i][],tem.z+dl[i][],tem.cnt+);
- dem.push(t);
- a[t.x][t.y][t.z]='#';
- //cout<<tem.x+dl[i][0]<<' '<<tem.y+dl[i][1]<<' '<<tem.z+dl[i][2]<<endl;
- }
- }
- }
- cout<<"Trapped!"<<endl;
- continue;
- en: printf("Escaped in %d minute(s).\n",tem.cnt);
- }
- }
POJ - 2251 Dungeon Master (搜索)的更多相关文章
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
随机推荐
- Linux bash篇(二 操作环境)
1.命令执行的顺序 (1).相对/绝对路径 (2).由alias找到的命令 (3).由bash内置的命令 (4).通过$PATH变量找到的第一个命令 2.第一篇讲到的bash在注销后就会无效,如果想保 ...
- 文本表格文件指定分隔符分列转Excel(java实现)
我的需求: 嗯,实习中遇到,需要过滤数据然后以指定的列名输出为excel 我是这样解决的: 写出到一个文本或者表格文件然后指定分隔符分列的输出excel,因为要设计去重处理. 我需要做的: 写一个文本 ...
- <E> 泛型
/* * 使用集合存储自定义对象并遍历 * 由于集合可以存储任意类型的对象,当我们存储了不同类型的对象,就有可能在转换的时候出现类型转换异常, * 所以java为了解决这个问题,给我们提供了一种机制, ...
- jvm入门及理解(四)——运行时数据区(堆+方法区)
一.堆 定义: Heap,通过new关键字创建的对象,都存放在堆内存中. 特点 线程共享,堆中的对象都存在线程安全的问题 垃圾回收,垃圾回收机制重点区域. jvm内存的划分: JVM内存划分为堆内存和 ...
- MODIS系列之NDVI(MOD13Q1)四:MRT单次及批次处理数据
前言: 本篇文章的出发点是因为之前接触过相关研究,困囧于该系列资料匮乏,想做一个系列.个人道行太浅,不足之处还请见谅.愿与诸君共勉. 数据准备: MODIS数据产品MOD13Q1—以2010年河南省3 ...
- Golang——详解Go语言代码规范
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Golang专题的第二篇,我们来看看Go的语言规范. 在我们继续今天的内容之前,先来回答一个问题. 有同学在后台问我,为什么说Gola ...
- C. 无穷的小数
单点时限: 1.0 sec 内存限制: 512 MB 在十进制下,我们能够很轻易地判断一个小数的位数是有穷的或无穷的,但是把这个小数用二进制表示出的情况下其有穷性和无穷性就会发生改变,比如 十进制下的 ...
- 机器学习常见面试题—支持向量机SVM
前言 总结了2017年找实习时,在头条.腾讯.小米.搜狐.阿里等公司常见的机器学习面试题. 支持向量机SVM 关于min和max交换位置满足的 d* <= p* 的条件并不是KKT条件 Ans: ...
- 将函数作为返回值的方法 - Python
有的时候,我们需要将函数作为返回值,以下为代码: def superfunc(): i = 0 def wrapper(): nonlocal i i +=1 return i return wrap ...
- poi导出word文档,doc和docx
maven <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency> <gro ...