题目大意:给一个三维图,可以前后左右上下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. Python中基本数据类型与对字符串处理的方法

    一.基本数据类型(int,bool,str) 1.基本数据类型: int 整数 整数 str字符串  一般不用来存放大量的数据 bool布尔值 用来判断(True,False) list 列表.用来存 ...

  2. table表格td内内容自动换行

    项目开发时,遇到问题:td内传入数据,全是字母,不会自动换行 一般字母数字/特殊符号的话,会被浏览器默认是一个字符串或者说一个单词,不会自动换行 所以需要设置一下,让表格内容自动换行. 1.给td标签 ...

  3. Python列表边遍历边修改问题解决方案:alist[:]

    最近在看python,遇到个简单的问题:删除列表中指定的重复元素,发现一些实用并且有趣的东西. 1.错误示范 alist = [1,1,2,2,3,3,2,2,1,1] for i in alist: ...

  4. int **a 和 int (*a)[]的区别

    关于理论知识隔壁们的教程说的很详细了我就不多赘述了.我这边主要贴一段代码来看看这两种东西使用上的区别到底在哪. #include <stdio.h> int main(int argc, ...

  5. C#中WinForm程序退出方法技巧总结[转]

      这篇文章主要介绍了C#中WinForm程序退出方法,实例总结了技巧退出WinForm程序窗口的各种常用技巧,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中WinForm程序退出方法技 ...

  6. 1.jdk的安装

    1.下载jdk放到某(E)盘底下的(java)某文件夹里 2.配置环境变量 (1)配置JAVA_HOME,CLASSPATH,PATH三个变量 其中JAVA_HOME必须的 JAVA_HOME=E:\ ...

  7. Linux远程桌面管理

    一: (1)查看用户  Linux系统root用户可强制踢制其它登录用户,首先可用w命令查看登录用户信息 [root@ylLinux~]:# w (2)强制踢人 命令格式:pkill -kill -t ...

  8. COGS 146. [USACO Jan08] 贝茜的晨练计划

    ★☆   输入文件:cowrun.in   输出文件:cowrun.out   简单对比时间限制:1 s   内存限制:32 MB 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运 ...

  9. Last_Errno: 1396

    Last_Errno: 1396   Last_Error: Error 'Operation CREATE USER failed for 'usera63'@'%'' on query. Defa ...

  10. swift 注解 (和java比照)@attribute name

    Attributes provide more information about a declaration or type. There are two kinds of attributes i ...