poj 2251 Dungeon Master (BFS 三维)
Is an escape possible? If yes, how long will it take?
Input
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! 不难,二维变三维,四个方向变六个方向。但是写搜索的时候我总是好粗心导致不必要的罚时,以后切记切记。
#include <iostream>
#include <cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
using namespace std; int h,m,n,res;
char ma[][][];
bool vis[][][];
int rx[]={,,,,,-};
int ry[]={,,,-,,};
int rz[]={,-,,,,}; struct node
{
int x,y,z;
int t;
}per;
int ex,ey,ez; bool judge(node a)
{
if(a.x>=&&a.x<m&&a.y>=&&a.y<n&&a.z>=&&a.z<h)
if(!vis[a.x][a.y][a.z])
if(ma[a.x][a.y][a.z]!='#')
return true;
return false;
} bool bfs()
{
queue<node>Q;
memset(vis,false,sizeof(vis));
Q.push(per);
vis[per.x][per.y][per.z]=true;
node tmp,next;
while(!Q.empty())
{
tmp=Q.front();
Q.pop();
if(tmp.x==ex&&tmp.y==ey&&tmp.z==ez)
{
res=tmp.t;
return true;
}
for(int i=;i<;i++)
{
next.x=tmp.x+rx[i];
next.y=tmp.y+ry[i];
next.z=tmp.z+rz[i];
next.t=tmp.t+;
if(judge(next))
{
Q.push(next);
vis[next.x][next.y][next.z]=true;
}
}
}
return false;
} int main()
{
while(~scanf("%d%d%d\n",&h,&m,&n)&&(h+m+n))
{
for(int q=;q<h;q++)
for(int i=;i<m;i++)
for(int j=;j<n;j++)
{
cin>>ma[i][j][q];
if(ma[i][j][q]=='S')
{
per.x=i;per.y=j;per.z=q;
per.t=;
}
else if(ma[i][j][q]=='E')
{
ex=i;ey=j;ez=q;
}
}
//
if(bfs()) printf("Escaped in %d minute(s).\n",res);
else printf("Trapped!\n");
}
return ;
}
poj 2251 Dungeon Master (BFS 三维)的更多相关文章
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
- 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最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- 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 /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...
随机推荐
- React Router页面传值的三种方法
文章地址:https://blog.csdn.net/qq_23158083/article/details/68488831
- Thymeleaf使用bootstrap及其bootstrap相关插件(二)
接上文http://www.cnblogs.com/conswin/p/7929772.html 接下来bootstrap-datepicker的简单使用. 1.引入添加js 和 css 2.然后是h ...
- 学习建一个spring-Mvc项目
学习建一个spring-Mvc项目 首先要有jdk1.8以上,spring,mybatis,以及整合jar包,tomcat ,然后配置环境(前面有配置得方法). 1)右键new project,--& ...
- Cmd管理员运行
Cmd管理员运行 C:\Windows\System32
- Notation, First Definitions 转 http://brnt.eu/phd/node9.html
LaTeX command Equivalent to Output style Remarks \textnormal{...} {\normalfont...} document font fam ...
- [IOS微信] PList文件解析,boost数据读取
最近在解析IOS版微信数据中的 mmsetting.archive 文件时,第一次接触到PList文件. 注:mmsetting.archive 不是一个标准的PList文件,其中含有汉字,并且很多 ...
- 【Java算法】获得一个随机字符串
package suanfa; import java.util.Random; public class RandomStr { public static String getRandomStr( ...
- firstchild.data与childNodes[0].nodeValue意思
x.firstchild.data:获取元素第一个子节点的数据: x.childNodes[0]::获取元素第一个子节点; x.childNodes[0].nodeValue.:也是获取元素第一个子节 ...
- prppppne2
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:/ ...
- TTL集成门电路工作原理和电压传输特性
集成电路(Integrated Circuit 简称IC):即把电路中半导体器件,电阻,电容以及连线等制作在一块半导体基片上构成一个完整的电路,并封装到一个管壳内 集成电路的有点:体积小,重量轻,可靠 ...