题目大意:给一个三维图,可以前后左右上下6种走法,走一步1分钟,求最少时间(其实就是最短路)

分析:这里与二维迷宫是一样的,只是多了2个方向可走,BFS就行(注意到DFS的话复杂度为O(6^n)肯定会TLE)

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <queue>
using namespace std;
#define N 33
#define M 30000
struct p
{
int ll,rr,cc,ans;
};
typedef struct p sp;
char m[N][N][N];
int mark[N][N][N];
int l,r,c;
int dir[][]={{,-,,,,},{,,-,,,},{,,,,-,}}; void test()//可跟踪mark数组debug程序
{
int i,j,k;
for (i=;i<l;i++)
{
for (j=;j<r;j++)
{
for (k=;k<c;k++)
{
printf("%d",mark[i][j][k]);
}
printf("\n");
}
printf("\n");
}
} int lawful(int x,int y,int z)
{
if (x<||x>=l||y<||y>=r||z<||z>=c||m[x][y][z]=='#')//判断合法性要完整!!!m[x][y][z]=='#'
{
return ;
}
else
{
return ;
}
} void solve()
{
sp point,point1;
int i,j,k;
queue<p> q;//要定义在函数内,使其每次测试都能清空队列!!!
memset(mark,,sizeof(mark));
for (i=;i<l;i++)
{
for (j=;j<r;j++)
{
for (k=;k<c;k++)
{
if (m[i][j][k]=='S')
{
point.ll=i;
point.rr=j;
point.cc=k;
point.ans=;
mark[i][j][k]=;//要记得给第一步步打上标记!!!
q.push(point);
}
}
}
}
while (!q.empty())
{
point=q.front();
q.pop();
if (m[point.ll][point.rr][point.cc]=='E')
{
printf("Escaped in %d minute(s).\n",point.ans);
return;
}
for (i=;i<;i++)
{
point1.ll=point.ll+dir[][i];
point1.rr=point.rr+dir[][i];
point1.cc=point.cc+dir[][i];
point1.ans=point.ans+;
if (lawful(point1.ll,point1.rr,point1.cc)&&!mark[point1.ll][point1.rr][point1.cc])//要记得判断标记!!!
{
q.push(point1);
mark[point1.ll][point1.rr][point1.cc]=;//记得更新标记!!!
// test();
}
}
}
printf("Trapped!\n"); } int main()
{
int i,j;
while (scanf("%d %d %d",&l,&r,&c))
{
if (l==&&r==&&c==)
{
break;
}
for (i=;i<l;i++)
{
for (j=;j<r;j++)
{
scanf("%s",m[i][j]);//输入字符的快捷方法
getchar();
}
}
solve();
} return ;
}

POJ-2251 三维迷宫的更多相关文章

  1. poj 2251 三维地图最短路径问题 bfs算法

    题意:给你一个三维地图,然后让你走出去,找到最短路径. 思路:bfs 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t struct node{ int x, y, z; int t;}; b ...

  2. POJ 2251 三维BFS(基础题)

    Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...

  3. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

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

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

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

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

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

  7. 【BFS】POJ 2251

    POJ 2251 Dungeon Master 题意:有一个地图,三维,走的方向是上下,左右,前后.问你最小步数从起始点走到出口. 思路:三维的BFS,就是多加一组状态,需要细心(不细心如我就找了半个 ...

  8. BFS POJ 2251 Dungeon Master

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

  9. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

  10. Dungeon Master poj 2251 dfs

    Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855 ...

随机推荐

  1. Sass基础(一)

    一.Sass语法规范 1.Sass老版本:通过tab键严格控制缩进:不带有任何分号和大括号:以“.sass”为扩展名. 2.Sass新语法格式(Scss):外形和css无差,以“.scss”为扩展名. ...

  2. [vijos]lxhgww的奇思妙想(长链剖分)

    题意 题目链接 Sol 长链剖分 又是一个用各种花式技巧优化的暴力 它的主要思想是:对于每个节点,把深度最深的子节点当做重儿子,它们之间的边当做重边 这样就会有一些非常好的轻质 所有链长总和是\(O( ...

  3. jQueryMobile(三)

    五].jQueryMobile工具栏 头部.尾部的定位:data-position='inline' 默认data-position='fixed' 固定在头部/尾部data-fullscreen=' ...

  4. mysql的三种索引

    MySQL中的索引分为3种: 1,主键索引:即用主键当唯一索引 2,常规索引:实现方式为B树和哈希表 3,全文索引:实现原理类似倒排索引,常用来查询字段中包含关键字 下面复习下B-TREE和hash- ...

  5. eclipse中安装thymeleaf插件完成thymeleaf模板中自动代码提示功能

    插件地址:https://github.com/thymeleaf/thymeleaf-extras-eclipse-plugin 页面有介绍如何使用:

  6. 只为更快、更省、更安全的 Azure CDN

    来来来!小编今天要公布一件大事啦: 经过最近一次更新,Azure CDN 高级版服务 HTTPS SSL 证书的申请方式有所改进啦,除了现有的 Azure CDN 代为申请证书外,还支持用户自己申请的 ...

  7. 变更hostname

    具有dns解析的主机名 # vim /etc/sysconfig/network ... HOSTNAME=webserver.mydomain.com ... # hostname webserve ...

  8. mouse事件在JQ中的应用(在动画与交互中用得比较多).

    mousedown与mouseup事件 用户交互操作中,最简单直接的操作就是点击操作,因此jQuery提供了一个mousedown的快捷方法可以监听用户鼠标按下的操作,与其对应的还有一个方法mouse ...

  9. 指定类型的成员XX”不支持实体LINQ。只有初始化,成员单位,和实体导航性能的支持。

    The specified type member 'DeleteFlag' is not supported in LINQ to Entities. Only initializers, enti ...

  10. bzoj4943 [Noi2017]蚯蚓排队

    题面:http://www.lydsy.com/JudgeOnline/upload/Noi2017D1.pdf 正解:字符串$hash$. 我在考场上写了个$map$的$hash$被卡成$40$分, ...