POJ.2251 Dungeon Master (三维BFS)

题意分析

你被困在一个3D地牢中且继续寻找最短路径逃生。地牢由立方体单位构成,立方体中不定会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能对角线移动并且迷宫四周坚石环绕。

若能逃离,则输出逃离需要的最短时间,否则输出Trapped!。

与二维BFS的差别在于,多了一个上下两层。所以除了先后左右移动,还要有上下移动,对于每个位置,总共有6种移动方式,将6种移动方式种可行的每次塞进队列里面就好了。

依旧还要记录歩数。此题没什么大坑。

代码总览

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define nmax 30
using namespace std;
char mp[nmax][nmax][nmax];
bool visit[nmax][nmax][nmax];
int Slevel,Sx,Sy,Elevel,Ex,Ey;
int spx[4] = {0,1,0,-1};
int spy[4] = {1,0,-1,0};
int spl[2] = {-1,1};
bool isout = false;
int l,r,c,ans;
typedef struct mes{
int level;
int x;
int y;
int steps;
}mes;
void changeVisit(mes temp)
{
visit[temp.level][temp.x][temp.y] = true;
return;
}
bool check(mes temp)
{
if(temp.level >= l || temp.level <0 || temp.x <0 || temp.x>=r || temp.y<0 || temp.y>=c || visit[temp.level][temp.x][temp.y] || mp[temp.level][temp.x][temp.y] == '#')
return false;
return true;
}
void bfs()
{
queue<mes> q;
while(!q.empty()) q.pop();
mes sta = {Slevel,Sx,Sy,0},temp,head;
q.push(sta);
changeVisit(sta);
while(!q.empty()){
head = q.front();q.pop();
if(head.level == Elevel && head.x == Ex && head.y == Ey){
isout = true;
ans = head.steps;
break;
}
for(int i = 0;i<4;++i){
temp.level = head.level;
temp.x = head.x + spx[i];
temp.y = head.y + spy[i];
temp.steps = head.steps + 1;
if(check(temp)){
q.push(temp);
changeVisit(temp);
}
}
for(int i = 0;i<2;++i){
temp.level = head.level + spl[i];
temp.x = head.x;
temp.y = head.y;
temp.steps = head.steps + 1;
if(check(temp)){
q.push(temp);
changeVisit(temp);
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d %d %d",&l,&r,&c) != EOF){
if( l == 0 && r == 0 && c == 0) break;
isout = false;
memset(mp,0,sizeof(mp));
memset(visit,0,sizeof(mp));
for(int i = 0;i<l;++i){
for(int j = 0;j<r;++j){
scanf("%s",mp[i][j]);
for(int k = 0;k<c;++k){
if(mp[i][j][k] == 'S'){
Slevel = i; Sx = j; Sy = k;
}else if(mp[i][j][k] == 'E'){
Elevel = i; Ex = j; Ey = k;
}
}
}
}
bfs();
if(isout == true){
printf("Escaped in %d minute(s).\n",ans);
}else{
printf("Trapped!\n");
}
}
return 0;
}

POJ.2251 Dungeon Master (三维BFS)的更多相关文章

  1. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  2. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. POJ - 2251 Dungeon Master 【BFS】

    题目链接 http://poj.org/problem?id=2251 题意 给出一个三维地图 给出一个起点 和 一个终点 '#' 表示 墙 走不通 '.' 表示 路 可以走通 求 从起点到终点的 最 ...

  4. (简单) 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 ...

  5. 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 ...

  6. POJ 2251 Dungeon Master【BFS】

    题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出. 学习的第一题三维的广搜@_@ 过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴) 另外这一题的输 ...

  7. BFS POJ 2251 Dungeon Master

    题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...

  8. POJ 2251 Dungeon Master(地牢大师)

    p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...

  9. 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 ...

随机推荐

  1. Python数据挖掘——基础知识

    Python数据挖掘——基础知识 数据挖掘又称从数据中 挖掘知识.知识提取.数据/模式分析 即为:从数据中发现知识的过程 1.数据清理 (消除噪声,删除不一致数据) 2.数据集成 (多种数据源 组合在 ...

  2. win10 redis安装教程

    下载解压,没什么好说的,在解压后的目录下有以下这些文件: 在 命令行 启动服务端 redis目录下执行: redis-server.exe redis.windows.conf 如果需要 开机启动:执 ...

  3. python2/3 发送https请求时,告警关闭方法

    问题: 使用Python3 requests发送HTTPS请求,已经关闭认证(verify=False)情况下,控制台会输出以下错误: InsecureRequestWarning: Unverifi ...

  4. daterangepicker时间段插件

    1.序言: daterangepicker是Bootstrap的一个时间组件,使用很方便 用于选择日期范围的JavaScript组件. 设计用于Bootstrap CSS框架. 它最初是为了改善报表而 ...

  5. USACO 2.4.4 Bessie Come Home 回家(最短路)

    Description 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只速度最快 ...

  6. 作业 20181030-3互评Alpha版本

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2323 组名:可以低头,但没必要 组长:付佳 组员:张俊余  李文涛  孙 ...

  7. 软件工程第九周psp

    1.PSP表格 2.进度条 3.饼状图 4.折线图

  8. CS小分队第一阶段冲刺站立会议(5月11日)

    昨日成果:完成了倒计时器的制作,为其添加了声音:并对扫雷游戏的失败添加了动态效果: 遇到的困难:把图片放入picturebox中无法改变图片的大小,音乐格式只能使用.wav,该格式音乐比较大,增加了整 ...

  9. WebSphere应用服务器内存泄漏探测与诊断工具选择最佳实践

    内存泄漏是比较常见的一种应用程序性能问题,一旦发生,则系统的可用内存和性能持续下降:最终将导致内存不足(OutOfMemory),系统彻底宕掉,不能响应任何请求,其危害相当严重.同时,Java堆(He ...

  10. DNS缓存服务器的配置步骤

    yum安装bind 编辑主配置文件/etc/named.conf 修改全局配置文件段        listen-on  port 53 {172.16.19.45;}; //allow-query ...