题目大意:给一个三维图,可以前后左右上下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. Visual Studio Code 入门教程

    Extensible and customizable.(可扩展的和可定制的,这是我喜欢它的原因) Want even more features? Install extensions to add ...

  2. Android实现异步的几种方法

    在Android项目中,有经验的开发人员都知道,一些耗时的IO操作等都必须在子线程中去操作,那么可以有哪些方法来开启子线程呢,一般可以使用Java中自带的几种方法,也可以使用Andorid特有的一些类 ...

  3. JDK、JRE、javac和JVM的关系

      .java为Java的源文件后缀,编写的代码需要写在.java文件中.     Javac编译器,用于读取Java源代码,并将其编译成字节代码.经过javac编译后形成.class,是字节码文件. ...

  4. 16 Javascript

    网上找网页模板: 1.HTML模板 2.BootStrap 前面内容总结 HTML 标签:块级,行内 CSS 后台管理布局 position: fixed  ---永远固定在窗口的某个位置 relat ...

  5. cesium 实现路径漫游功能

    功能:路径漫游. 详细:绘制多个路径,删除路径,漫游路径选择,路径漫游. ①绘制路径 ②保存路径 ③路径切换 ④路径漫游 ⑤路径删除 ⑥删除完成

  6. selenium入门14 窗口切换

    窗口切换: 当前窗口句柄 current_window_handle 所有的窗口句柄 window_handles 切换窗口 switch_to_window() #coding=utf-8 #切换窗 ...

  7. DOM(十四):代理检测和事件处理(跨浏览器)

    一.检测 用于用户代理检测,检测范围包括浏览器引擎.平台.Windows.移动设备和游戏系统等 /* *用户代理检测脚本,检测范围包括浏览器引擎.平台.Windows.移动设备和游戏系统 */ var ...

  8. IOS 单例模式(非ARC)

    singleton_h :连接字符串和参数 // ## : 连接字符串和参数 #define singleton_h(name) + (instancetype)shared##name; #defi ...

  9. hdu-2844&&POJ-1742 Coins---多重背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目大意: Tony想要买一个东西,他只有n中硬币每种硬币的面值为a[i]每种硬币的数量为c[ ...

  10. R 语言爬虫 之 cnblog博文爬取

    Cnbolg Crawl a). 加载用到的R包 ##library packages needed in this case library(proto) library(gsubfn) ## Wa ...