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 ...
随机推荐
- sqlalchemy + alembic数据迁移
需要安装的包工具 pip install pymysql pip install sqlalchemy pip install alembic 创建表 新建models.py from sqlal ...
- cookie、session、csrf
cookie的设置和获取 import time from tornado.web import RequestHandler class IndexHandle(RequestHandler): d ...
- tornado的ORM
tornado的ORM 安装sqlalchemy和pymysql pip install sqlalchemy pip install pymysql 连接数据库 from sqlalchemy im ...
- SQL基础系列(2)-内置函数--转载w3school
1. 日期函数 Mssql: SELECT GETDATE() 返回当前日期和时间 SELECT DATEPART(yyyy,OrderDate) AS OrderYear, DATEPART( ...
- 怎么入门python?不懂你别瞎尝试,看看大佬怎么说
学习任何一门语言都是从入门,通过不间断练习达到熟练水准.虽然万事开头难,但好的开始是成功的一半,今天这篇文章就来谈谈怎么入门python? 在开始学习python之前,你需要确定好学习计划和方式 比如 ...
- Pormetheus(一)
(1)Prometheus由来普罗米修斯的灵感来自于谷歌的Borgmon.它最初是由马特·t·普劳德(Matt T. Proud)作为一个研究项目开发的,普劳德曾是谷歌(google)的一名雇员.在普 ...
- O - Navigation System CodeForces - 1321D
题目大意:有一个导航系统,会根据你当前的位置,规划到目的地的最短路线,给你一个有向图,和一条行驶路径,问你导航重新规划路径的最大次数和最小次数. 读题的时候题意特别不理解,何为最大次数,何为最小次数? ...
- Python 如何移除旧的版本特性,如何迎接新的特性?
2020 年 4 月 20 日,Python 2 的最后一个版本 2.7.18 发布了,这意味着 Python 2 是真正的 EOL(end of life)了,一个时代终于落幕了. Python 2 ...
- DEV gridview 合并单元格
private void gv_docargo_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e ...
- Python - 生成随机验证码的3种实现方式
生成6位随机验证码的3种实现方式如下: 1. 简单粗暴型:所有数字和字母都放入字符串: 2. 利用ascii编码的规律,遍历获取字符串和数字的字符串格式: 3. 引用string库. 方法1代码: i ...