题目链接

http://poj.org/problem?id=2251

题意

给出一个三维地图 给出一个起点 和 一个终点

‘#’ 表示 墙 走不通

‘.’ 表示 路 可以走通

求 从起点到终点的 最短路径 走不通输出 Trapped!

方向 是可以 上,下,东南西北 六个方向

思路

BFS板子题 注意要记录 S 和 E 的位置 因为不一定是固定的 要标记访问

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 3e1 + 5;
const int MOD = 1e9 + 7; int G[maxn][maxn][maxn];
int v[maxn][maxn][maxn]; int sx, sy, sz, ex, ey, ez; int Move[6][3]
{
1, 0, 0,
-1, 0, 0,
0, 1, 0,
0,-1, 0,
0, 0, 1,
0, 0,-1,
}; struct node
{
int x, y, z;
int step;
}; int ans; int c, n, m; bool ok(int x, int y, int z)
{
if (x < 0 || x >= c || y < 0 || y >= n || z < 0 || z >= m || G[x][y][z] || v[x][y][z])
return false;
return true;
} void bfs()
{
node tmp;
tmp.x = sx;
tmp.y = sy;
tmp.z = sz;
tmp.step = 0;
queue <node> q;
q.push(tmp);
v[sx][sy][sz] = 1;
while (!q.empty())
{
node u = q.front(), V;
q.pop();
if (u.x == ex && u.y == ey && u.z == ez)
{
ans = u.step;
return;
}
for (int i = 0; i < 6; i++)
{
V.x = u.x + Move[i][0];
V.y = u.y + Move[i][1];
V.z = u.z + Move[i][2];
if (ok(V.x, V.y, V.z))
{
V.step = u.step + 1;
q.push(V);
v[V.x][V.y][V.z] = 1;
}
}
}
} int main()
{
while (scanf("%d%d%d", &c, &n, &m) && (c || n || m))
{
CLR(G, 0);
CLR(v, 0);
string s;
for (int i = 0; i < c; i++)
{
for (int j = 0; j < n; j++)
{
cin >> s;
for (int k = 0; k < m; k++)
{
if (s[k] == 'S')
{
sx = i;
sy = j;
sz = k;
G[i][j][k] = 0;
}
else if (s[k] == 'E')
{
ex = i;
ey = j;
ez = k;
G[i][j][k] = 0;
}
else if (s[k] == '.')
G[i][j][k] = 0;
else if (s[k] == '#')
G[i][j][k] = 1;
}
}
}
ans = -1;
bfs();
if (ans == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
}
}

POJ - 2251 Dungeon Master 【BFS】的更多相关文章

  1. POJ 2251 Dungeon Master【BFS】

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

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

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

  3. POJ 2251 Dungeon Master (三维BFS)

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

  4. POJ 2251 Dungeon Master【三维BFS模板】

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...

  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。

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

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

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

  8. BFS POJ 2251 Dungeon Master

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

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

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

随机推荐

  1. mac xampp命令行调用mysql

    参考 http://www.cnblogs.com/machao/p/6206483.html 直接mysql不行,显示command not found 调用 sudo ln -s /applica ...

  2. windows核心编程 DLL技术 【转】

    注:本文章转载于网络,源地址为:http://blog.csdn.net/ithzhang/article/details/7051558 本篇文章将介绍DLL显式链接的过程和模块基地址重定位及模块绑 ...

  3. glsl镜面水倒影的实现[转]

    http://blog.sina.com.cn/s/blog_78ea87380101ejbf.html 使用两相机,一个master相机, 主要负责场景的渲染, 另一个rtt相机, 和master相 ...

  4. Hadoop部署启动异常问题排查

    hadoop的日志目录(/home/hadoop/app/hadoop-2.6.4/logs) 1.hadoop启动不正常用浏览器访问namenode的50070端口,不正常,需要诊断问题出在哪里: ...

  5. 同步数据库数据到ES中代码

    多节点部署保证HA,分布式锁代码 public class DistributedLock implements Watcher,Runnable{ private static final Logg ...

  6. 【音乐App】—— Vue-music 项目学习笔记:推荐页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 上一篇总结了项目概述.项目准备.页面骨架搭建.这一篇重点梳理推荐页面开发.项目github地址:https://github.com/66We ...

  7. APT攻击:91%的攻击是利用电子邮件

    一封假冒的"二代医疗保险补充保险费扣费说明",导致上万家中小型企业的资料被窃;一封伪装银行交易纪录的钓鱼信件,让韩国爆发史上最大黑客攻击. APT攻击通常会以电子邮件的形式出现,邮 ...

  8. MYSQL 的optimize怎么用

    当对表有大量的增删改操作时,需要用optimize对表进行优化.可以减少空间与提高I/O性能,命令optimize table tablename;假如有foo表且存储引擎为MyISAM. mysql ...

  9. setTimeout()基础/setInterval()基础

    JavaScript提供定时执行代码的功能,叫做定时器(timer),主要由setTimeout()和setInterval()这两个函数来完成.它们向任务队列添加定时任务.初始接触它的人都觉得好简单 ...

  10. 本地aar文件引用

    有时须要使用第三方的aar库.或是project源码越来越大.项目内分工须要或出于模块化考虑.须要引用aar文件. arr就像C/C++中的静态库. 怎样建一个aar.网上的文章非常多,这里不再重述. ...