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: ...
随机推荐
- 采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序(多activity)
问题: 多activity中退出整个程序,例如从A->B->C->D,这时我需要从D直接退出程序. 网上资料:{ finish()和system(0)都只能退出单个activity. ...
- DirectX9完全面向对象框架
#pragma once #define UNICODE //Direct3D lib #include<d3d9.h> #include<d3dx9.h> #pragma c ...
- eshop-环境配置
1. iptables # Generated by iptables-save v1. :: #*filter #:INPUT ACCEPT [:] #:FORWARD ACCEPT [:] #:O ...
- 云时代架构阅读笔记六——Java内存模型详解(二)
承接上文:云时代架构阅读笔记五——Java内存模型详解(一) 原子性.可见性.有序性 Java内存模型围绕着并发过程中如何处理原子性.可见性和有序性这三个特征来建立的,来逐个看一下: 1.原子性(At ...
- 《动手学深度学习》系列笔记—— 1.2 Softmax回归与分类模型
目录 softmax的基本概念 交叉熵损失函数 模型训练和预测 获取Fashion-MNIST训练集和读取数据 get dataset softmax从零开始的实现 获取训练集数据和测试集数据 模型参 ...
- js 运行机制简单了解
一.如何理解 JS 的单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事.那么,为什么JavaScript不能有多个线程呢?这样能提高效率啊. JavaScrip ...
- 158-PHP strstr函数输出第一次出现字符串的位置到字符串结尾的所有字符串
<?php $str='PHP is a very good programming language!'; //定义一个字符串 echo '第一次出现字母l的位置到字符串结尾的所有字符串'.s ...
- 100-PHP二维数组的元素输出三
<?php $stu=array(array(76,87,68), array(65,89,95), array(90,80,66), array(90,95,65)); //定义一个二维数组 ...
- Pyinstaller的安装及简单使用
(1)安装: 用传统的pip install pyinstaller出错,在https://pypi.org/project/PyInstaller/#files上下载PyInstaller-3.4. ...
- JS图片多个上传,并压缩为Base64
首先是JS 批量上传 HTML <div id="Pic_pass"> <p style="font-size: 20px;font-weight: b ...