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. Django的简介

    一.MTV模型 Django的MTV模式: Model(模型):和数据库相关的.负责业务对象与数据库的对象(ORM) Template(,模板):放所有的HTML文件 模板语法:目的是将变量(数据库内 ...

  2. 「日常训练」The Necklace(UVA-10054)

    代码 for(int i=0; i!=n; ++i) { int u = cin.nextInt(); int v = cin.nextInt(); edges.add(new Edge(u,v)); ...

  3. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

  4. request,logging,ConfigParser——接口框架

    做一个将参数和用例分开放置,并且输出log的接口测试框架 我的框架如下所示 Log文件用来设置log输出文件,需要时可以在用例内调用输出,config用来填写一切需要的参数信息,jiekou_post ...

  5. 二叉树的深度<java版>

    二叉树的结构 二叉树是比较常见的一种的一种数据结构. 首先看看二叉树的数据结构: //由左节点和右节点以及一个节点值构成 public class TreeNode{ TreeNode leftNod ...

  6. 利用Tensorflow进行自然语言处理(NLP)系列之二高级Word2Vec

    本篇也同步笔者另一博客上(https://blog.csdn.net/qq_37608890/article/details/81530542) 一.概述 在上一篇中,我们介绍了Word2Vec即词向 ...

  7. 如何把node更新到最新的稳定版本

    先装n,再用n把node升级到最新稳定版 $ npm install -g n $ n stable

  8. 扩展Lucas定理 扩展Lucas板子

    题意概述:多组询问,给出N,K,M,要求回答C(N,K)%M,1<=N<=10^18,1<=K<=N,2<=M<=10^6 分析: 模数不为质数只能用扩展Lucas ...

  9. 几个好用的php函数

    几个好用的php函数 1.PHP加密解密 PHP加密和解密函数可以用来加密一些有用的字符串存放在数据库里,并且通过可逆解密字符串,该函数使用了base64和MD5加密和解密. function enc ...

  10. 附加题程序找bug

    private: void Resize(int sz){ ){ return; } if(maxSize != sz){ T *arr = new T[sz]; if(arr == NULL){ r ...