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: ...
随机推荐
- 编程练习:实现树的层次遍历 (CVTE笔试)
直接层次遍历是比较简单的,但是题目要求的分层打印,这就变得稍微有些麻烦 我是采用两个队列的方法实现. 1.将树结构入队列1. 2.当队列1和队列2都不为空的时候,则一直循环. 3.当队列1不为空的时候 ...
- Excel中神奇的vlookup函数之基础应用
1.问题: 如下示例,需要将右边的表格匹配上对应工号的销售额. 这属于vlookup函数最基础的单条件匹配应用,左边表称为A表.右边表称为B表. 2.vlookup函数套路介绍 vlookup ...
- Spring Boot -- 认识Spring Boot
在前面我们已经学习过Srping MVC框架,我们需要配置web.xml.spring mvc配置文件,tomcat,是不是感觉配置较为繁琐.那我们今天不妨来试试使用Spring Boot,Sprin ...
- 序列号导出到csv的实现
//导出到csv public function exportCsvByIds($ids){ header("Content-type:text/html;charset=utf-8&quo ...
- Java连载67-深入一维数组、main方法中的args参数详解
一.复习了一维数组,还复习了强制类型转换的注意点. package com.bjpowernode.java_learning; public class D67_1_GoDeepIntoArrays ...
- Spring的JDBC的使用(配置和CRUD)
导包: Spring的JDBC模板的使用 一.默认连接池 创建数据库 create database spring4; use spring4; create table account(id int ...
- C语言循环
C 练习实例1 #include<stdio.h> int main() { int i,j,k; printf("\n"); //此处巧妙的利用循环次数和四个相等的关 ...
- 012.Oracle数据库,字符串文本大小写转换,转大写,转小写,首字母大写
/*转大写*/ SELECT UPPER(TITLE_EN) FROM ME_EO WHERE ( ISSUE_DATE BETWEEN to_date( '2017-02-04', 'yyyy-MM ...
- 洛谷题解P1047 校门外的树
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,…,L,都种有 ...
- torch.nn.Conv2d()使用
API 输入:[ batch_size, channels, height_1, width_1 ] Conv2d输入参数:[ channels, output, height_2, width_2 ...