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!
//因为是三维寻找路径,又是最短路径,所以用bfs
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
char a[][][];
int b[][][];
int L,R,C;
int f[][]={{,-,, ,, },
{, ,,-,, },
{, ,, ,,-}};
int s2[][][];
int flag;
struct Knot
{
int x,y,z;
int step;
};
Knot c,d;
bool search1(int x,int y,int z)
{
return (x>=&&x<=L&&y>=&&y<=R&&z>=&&z<=C);
}
int bfs(int si,int sj,int sk)
{
queue<Knot>s;
c.x=si;
c.y=sj;
c.z=sk;
c.step=;
s.push(c);
while (!s.empty())
{
d=s.front();
s.pop();
c.step=d.step+;
for (int i=;i<;i++)
{
c.x=d.x+f[][i];
c.y=d.y+f[][i];
c.z=d.z+f[][i];
if (search1(c.x,c.y,c.z)&&!b[c.x][c.y][c.z]&&a[c.x][c.y][c.z]!='#')
{
if (a[c.x][c.y][c.z]=='E')
return c.step;
b[c.x][c.y][c.z]=;
s.push(c);
}
}
}
return -;
}
int main()
{
int i,j,k;
int si,sj,sk;
while (cin>>L>>R>>C&&(L!=||R!=||C!=))
{
memset(b,,sizeof(b));
for (i=;i<=L;i++)
for (j=;j<=R;j++)
for (k=;k<=C;k++)
{
cin>>a[i][j][k];
if (a[i][j][k]=='S')
{
si=i;
sj=j;
sk=k;
}
}
flag=bfs(si,sj,sk);
if (flag==-)
cout << "Trapped!" << endl;
else
cout << "Escaped in " << flag << " minute(s)." << endl; }
return ;
}
Dungeon Master (三维bfs)的更多相关文章
- 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 ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- Dungeon Master(三维bfs)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 棋盘问题(DFS)& Dungeon Master (BFS)
1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...
- Dungeon Master (简单BFS)
Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...
- POJ 2252 Dungeon Master 三维水bfs
题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...
随机推荐
- PHP培训教程 php几个不起眼儿的小技巧
说是不起眼儿的小技巧,其实应该说是不常用的常规应用吧.很多事情就是这样,知道是一马事儿,会用是一马事儿,精习又是另外一马事儿.而成为高手更是需要扎实的基本功. str_repeat 重复输出字符串就靠 ...
- 建立PHP的本地调试环境PHP基础教程
对于windows平台而言,PHP供给了一个安装程序,但是这个安装程序是不能直接运行来建立个人服务器平台的,而是必需嵌进现有的服务器平台,如:Windows NT下的IIS或Windows下的PWS, ...
- 获取当前国家与ip地址
JS获取当前国家示例: <script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"& ...
- Elastic-Job介绍
1 什么是分布式任务调度 什么是分布式?当前软件的架构正在逐步转变为分布式架构,将单体结构分为若干服务,服务之间通过网络交互来完成用户的业务处理,如下图,电商系统为分布式架构,由订单服务.商品服务.用 ...
- adaptiveThreshold(自适应阈值)
void adaptiveThreshold(InputArray src, OutputArray dst, double maxValue, int adaptiveMethod, int thr ...
- css实现不定宽高的div水平、垂直居中
一共有三个方案: 1,第一种方案主要使用了css3中transform进行元素偏移,效果非常好 这方法功能很强大,也比较灵活,不仅仅局限在实现居中显示. 兼容方面也一样拿IE来做比较,第二种方法IE ...
- vim编辑器快捷键
光标控制命令 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开 ...
- 第五周课程总结&试验报告 (三)
课程总结 一,类的继承格式 1.在 Java 中通过 extends 关键字可以申明一个类是从另外一个类继承而来的,一般形式如下: class 父类 {} class 子类 extends 父类 {} ...
- legend3---laravel验证码使用
legend3---laravel验证码使用 一.总结 一句话总结: 1.先用composer下载好captcha扩展:配置好composer.json然后运行composer update 2.引入 ...
- MySQL表碎片清理
MySQL大表清理 生产环境data库业务表base_data大小:500G,data_free:31G mysql> SELECT table_schema,table_name,data_f ...