POJ 2251:Dungeon Master
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 20687 | Accepted: 8004 |
Description
the maze is surrounded by solid rock on all sides.
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!
一个Test给你几个地图,给了你一个三维的模型,其实质就是之前人在格子里走路只能是上下左右四个方向,这回如果你在中间的地图里的话,又多了两个方向,就叫成前后吧,其实质是bfs,中规中矩的一个模式而已。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
using namespace std; int L,R,C;
char value[50][50][50];
int bushu[50][50][50]; int bfs(int i,int j,int k)
{
int flag=0; queue<int>x;
queue<int>y;
queue<int>z; x.push(i);
y.push(j);
z.push(k); while(x.size())
{
int temp1=x.front();
int temp2=y.front();
int temp3=z.front(); x.pop();
y.pop();
z.pop(); if(temp1-1>=1&&value[temp1-1][temp2][temp3]=='.'&&!bushu[temp1-1][temp2][temp3])
{
x.push(temp1-1);
y.push(temp2);
z.push(temp3);
bushu[temp1-1][temp2][temp3]=bushu[temp1][temp2][temp3]+1;
} if(temp1+1<=L&&value[temp1+1][temp2][temp3]=='.'&&!bushu[temp1+1][temp2][temp3])
{
x.push(temp1+1);
y.push(temp2);
z.push(temp3);
bushu[temp1+1][temp2][temp3]=bushu[temp1][temp2][temp3]+1;
}
if(temp2-1>=1&&value[temp1][temp2-1][temp3]=='.'&&!bushu[temp1][temp2-1][temp3])
{
x.push(temp1);
y.push(temp2-1);
z.push(temp3);
bushu[temp1][temp2-1][temp3]=bushu[temp1][temp2][temp3]+1;
}
if(temp2+1<=R&&value[temp1][temp2+1][temp3]=='.'&&!bushu[temp1][temp2+1][temp3])
{
x.push(temp1);
y.push(temp2+1);
z.push(temp3);
bushu[temp1][temp2+1][temp3]=bushu[temp1][temp2][temp3]+1;
}
if(temp3-1>=1&&value[temp1][temp2][temp3-1]=='.'&&!bushu[temp1][temp2][temp3-1])
{
x.push(temp1);
y.push(temp2);
z.push(temp3-1);
bushu[temp1][temp2][temp3-1]=bushu[temp1][temp2][temp3]+1;
}
if(temp3+1<=C&&value[temp1][temp2][temp3+1]=='.'&&!bushu[temp1][temp2][temp3+1])
{
x.push(temp1);
y.push(temp2);
z.push(temp3+1);
bushu[temp1][temp2][temp3+1]=bushu[temp1][temp2][temp3]+1;
}
if(value[temp1-1][temp2][temp3]=='E'||value[temp1+1][temp2][temp3]=='E'||value[temp1][temp2-1][temp3]=='E'||
value[temp1][temp2+1][temp3]=='E'||value[temp1][temp2][temp3-1]=='E'||value[temp1][temp2][temp3+1]=='E')
{
flag=1;
return bushu[temp1][temp2][temp3]+1;
}
}
return 0;
} void solve()
{
int i,j,k;
for(i=L;i>=1;i--)
{
for(j=R;j>=1;j--)
{
for(k=C;k>=1;k--)
{
if(value[i][j][k]=='S')
{
int temp=bfs(i,j,k);
if(temp==0)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<temp<<" minute(s)."<<endl;
return;
}
}
}
}
} int main()
{
int i,j,k;
while(scanf("%d%d%d",&L,&R,&C))
{
if(L+R+C==0)
break;
for(i=1;i<=L;i++)
{
for(j=1;j<=R;j++)
{
scanf("%s",value[i][j]+1);
}
}
memset(bushu,0,sizeof(bushu));
solve();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2251:Dungeon Master的更多相关文章
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- 【一本通1248:Dungeon Master&&洛谷UVA532 Dungeon Master】
若不会广搜转向[广搜] [题目描述] 这题是一个三维的迷宫题目,其中用‘.’表示空地,‘#’表示障碍物,‘S’表示起点,‘E’表示终点,求从起点到终点的最小移动次数,解法和二维的类似,只是在行动时除了 ...
- [ACM训练] 算法初级 之 搜索算法 之 深度优先算法DFS (POJ 2251+2488+3083+3009+1321)
对于深度优先算法,第一个直观的想法是只要是要求输出最短情况的详细步骤的题目基本上都要使用深度优先来解决.比较常见的题目类型比如寻路等,可以结合相关的经典算法进行分析. 常用步骤: 第一道题目:Dung ...
- 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(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 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: ...
随机推荐
- 最简单-转换MBR为GPT
Windows Server 2016可能没有这个 mbr2gpt 这个软件,可以从Windows 10 的C:\Windows\System32 目录下面复制到 Windows Server 201 ...
- systemctl常规应用
以samba为例——常用项 #systemctl start smb ——在smb服务没有启动的情况下启动这项服务. #systemctl restart smb ——在修该过相应的配 ...
- Day6-T2
原题目 给你一个长度为n的序列A,请求出最大的一对数(Ai ,Aj),使Ai&Aj最大. 第一行为n,接下来n行,每一个数表示Ai. 输出最大的“and”. S1: Input: Output ...
- redis数据导入与导出以及配置使用
最近在研究redis 遇到redis requires Ruby version >= 2.2.2问题 解决办法是 先安装rvm,再把ruby版本提升至2.3.3 1.安装curl sudo y ...
- 谈谈HashSet的存储原理及为什么重写equals必须重写hashcode方法
HashSet的存储原理: 1.将要传入的数据根据系统的hash算法得到一个hash值: 2.根据hash值可以得出该数据在hash表中的位置: 3.判断该位置上是否有值,没有值则把数据插入进来:如果 ...
- PHP计划任务
server 2008:D:\SOFT_PHP_PACKAGE\php\php-cgi.exe -f D:\wwwroot\tlbuyuncom\wwwroot\Up_Data.phpPHP路径 -f ...
- Window Server 2019 配置篇(2)- 在window server core上安装网络跟DHCP服务
上一篇我们已经建立了自己的域服务器 之后我们将安装一个window server core,也就是没有GUI只有命令行的window server,并在其上安装网络服务和DHCP 首先创建一个新的虚拟 ...
- P1055 集体照
P1055 集体照 转跳点:
- P 1035 插入与归并
转跳点 :
- 2.7 学习总结 之【Android】java To Kotlin 一(初识)
一.Kotlin 的方便之处 1.Kotlin 可以直接使用id来呼叫操控相应的控件( textView.text = "0" ) java( TextView textVie ...