POJ-2251 Dungeon Master (BFS模板题)
Is an escape possible? If yes, how long will it take?
Input
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
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! 题目分析:BFS模板题。
# include<iostream>
# include<cstdio>
# include<algorithm>
# include<cstring>
# include<queue>
using namespace std;
struct node
{
int x,y,z,t;
node(int a,int b,int c,int d):x(a),y(b),z(c),t(d){}
bool operator < (const node &a) const {
return t>a.t;
}
};
char p[][][],vis[][][];
int l,r,c;
int d[][]={{,,},{,,-},{,,},{-,,},{,,},{,-,}};
void bfs(int sx,int sy,int sz)
{
priority_queue<node>q;
memset(vis,,sizeof(vis));
vis[sx][sy][sz]='';
q.push(node(sx,sy,sz,));
while(!q.empty())
{
node u=q.top();
q.pop();
if(p[u.x][u.y][u.z]=='E'){
printf("Escaped in %d minute(s).\n",u.t);
return ;
}
for(int i=;i<;++i){
int nx=u.x+d[i][],ny=u.y+d[i][],nz=u.z+d[i][];
if(nx>=&&nx<r&&ny>=&&ny<c&&nz>=&&nz<l&&!vis[nx][ny][nz]&&p[nx][ny][nz]!='#'){
vis[nx][ny][nz]='';
q.push(node(nx,ny,nz,u.t+));
}
}
}
printf("Trapped!\n");
}
int main()
{
while(scanf("%d%d%d",&l,&r,&c),l+r+c)
{
int sx,sy,sz;
for(int k=;k<l;++k){
for(int i=;i<r;++i){
for(int j=;j<c;++j){
cin>>p[i][j][k];
if(p[i][j][k]=='S')
sx=i,sy=j,sz=k;
}
}
}
bfs(sx,sy,sz);
}
return ;
}
代码如下:
POJ-2251 Dungeon Master (BFS模板题)的更多相关文章
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- 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 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- 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)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- nginx 配置https没有ssl_module以及一些错误
一:开始Nginx的SSL模块 1.1 Nginx如果未开启SSL模块,配置Https时提示错误 1 nginx: [emerg] the "ssl" parameter requ ...
- 删除github上个人的repositories的操作步骤
- Java HSSFworkbook,XSSFworkbook,SXSSFworkbook区别简述
Java HSSFworkbook,XSSFworkbook,SXSSFworkbook区别简述 一.HSSFworkbook,XSSFworkbook,SXSSFworkbook区别简述 用Java ...
- MQ内存消耗与积压分析
[root@iZ23nn1p4mjZ logs]# rabbitmqctl status Status of node rabbit@iZ23nn1p4mjZ ... [{pid,15425}, {r ...
- Python descriptor 以及 内置property()函数
Python Descriptor 1, Python Descriptor是这样一个对象 它按照descriptor协议, 有这样的属性之一 def __get__(self, obj, type ...
- 2017.7.4 ACM校内赛 Round 2
这是一个向导 A - hdu 3652 B - bzoj 4152 C - bzoj 2429 D - bzoj 1087 E - bzoj 1566 F - bzoj 4043 G - bzoj 1 ...
- static理解
static 修饰的变量称为类变量或全局变量或成员变量,在类被加载的时候成员变量即被初始化,与类关联,只要类存在,static变量就存在. 一个static变量单独划分一块存储空间,不与具体的对象绑定 ...
- 关于定时器、波特率、TH和TL值的计算
假设晶振位6MHZ,定时10ms 单片机系统晶振频率为6mhz,系统时钟频率 (也是计时脉冲频率)为500KHZ,一个脉冲周期2us ,10ms是5000个脉冲,因此TMOD=0X01;TH0=(65 ...
- 帮助大家理解java中的随机和继承,动态绑定.
package com.ykmimi.javabianchengsixiang; /** * 形状的继承 随机形状生成器 * @author ukyor */ import java.util.Ran ...
- 【转载】可被路由的协议 & 路由协议 & 不可被路由的协议 的区别
原文地址:可被路由的协议 & 路由协议 & 不可被路由的协议 的区别 术语routed protocol(可被路由的协议)和routing protocol(路由协议)经常被混淆.可被 ...