POJ 2251 Dungeon Master(三维空间bfs)
题意:三维空间求最短路,可前后左右上下移动。
分析:开三维数组即可。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 30 + 10;
char pic[MAXN][MAXN][MAXN];
bool vis[MAXN][MAXN][MAXN];
int sx, sy, sz;
int dr[] = {0, 0, 1, -1, 0, 0};
int dc[] = {1, -1, 0, 0, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};
int L, R, C;
bool judge(int x, int y, int z){
return x >= 0 && x < R && y >= 0 && y < C && z >= 0 && z < L;
}
int bfs(){
queue<int> x, y, z, step;
x.push(sx), y.push(sy), z.push(sz), step.push(0);
vis[sz][sx][sy] = true;
while(!x.empty()){
int tmpx = x.front(); x.pop();
int tmpy = y.front(); y.pop();
int tmpz = z.front(); z.pop();
int tmpstep = step.front(); step.pop();
for(int i = 0; i < 6; ++i){
int tx = tmpx + dr[i];
int ty = tmpy + dc[i];
int tz = tmpz + dz[i];
if(judge(tx, ty, tz)){
if(pic[tz][tx][ty] == 'E') return tmpstep + 1;
if(pic[tz][tx][ty] != '#' && !vis[tz][tx][ty]){
vis[tz][tx][ty] = true;
x.push(tx);
y.push(ty);
z.push(tz);
step.push(tmpstep + 1);
}
}
}
}
return -1;
}
int main(){
while(scanf("%d%d%d", &L, &R, &C) == 3){
if(!L && !R && !C) return 0;
memset(vis, false, sizeof vis);
for(int i = 0; i < L; ++i){
for(int j = 0; j < R; ++j){
scanf("%s", pic[i][j]);
for(int k = 0; k < C; ++k){
if(pic[i][j][k] == 'S'){
sz = i, sx = j, sy = k;
}
}
}
}
int ans = bfs();
if(ans == -1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}
POJ 2251 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 - 2251 Dungeon Master 【BFS】
题目链接 http://poj.org/problem?id=2251 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...
- poj 2251 Dungeon Master(bfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- (简单) POJ 2251 Dungeon Master,BFS。
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- POJ 2251 Dungeon Master【BFS】
题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出. 学习的第一题三维的广搜@_@ 过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴) 另外这一题的输 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- 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 ...
随机推荐
- 解决maven项目创建过慢的问题,一直resolving dependencies...
##方法一: 1.在创建项目时设置其Properties (我大多在创建项目时 就解决这个问题) 创建项目的时候,在Properties属性面板中添加一项:archetypeCatalog = int ...
- Day2-G-Sticks-POJ1011
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- java创建线程方式
1.继承Thread类 public class ThreadCreator extends Thread{ public static void main(String[] args) { //第一 ...
- 使用 CAS 在 Tomcat 中实现单点登录 http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/
developerWorks 中国 技术主题 Open source 文档库 使用 CAS 在 Tomcat 中实现单点登录 单点登录(Single Sign On , 简称 SSO )是目前比较流行 ...
- Spring JMSTemplate 与 JMS 原生API比较
博客分类: JMS Spring 2.x JMSUtil与Spring JmsTemplate的对比 Author:信仰 Date:2012-4-20 未完待续,截止日期2012-4-20 从以下 ...
- pip install .whl文件时is not a supported wheel on this platform.解决方法
首先,在python中输入import pip和print(pip.pep425tags.get_supported()),从而获取pip支持的文件名和版本. somnus@somnus-HP-Pa ...
- VUE - 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
created() { var that=this axios.get('http://jsonplaceholder.typicode.com/todos') .then( ...
- [笔记]ul>li>a做分布时, 让其居中显示效果
结构: <div id="page"> <ul> <li><a href="#">首页</a>< ...
- 用Python实现简单的服务器【新手必学】
如何实现服务器... socket接口是实际上是操作系统提供的系统调用.socket的使用并不局限于Python语言,你可以用C或者JAVA来写出同样的socket服务器,而所有语言使用socket的 ...
- 021、MySQL变量的使用,在MySQL中创建存储过程,并添加变量
#编写一个存储过程 CREATE PROCEDURE ShowDate ( ) BEGIN #变量定义 ); #变量赋值 set m_str1 = '曾经沧海难为水'; #输出当前时间 SELECT ...