POJ - 2251 Dungeon Master 【BFS】
题目链接
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】的更多相关文章
- POJ 2251 Dungeon Master【BFS】
题意:给出一个三维坐标的牢,给出起点st,给出终点en,问能够在多少秒内逃出. 学习的第一题三维的广搜@_@ 过程和二维的一样,只是搜索方向可以有6个方向(x,y,z的正半轴,负半轴) 另外这一题的输 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2251 Dungeon Master【三维BFS模板】
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 Desc ...
- 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 ...
- (简单) 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 ...
- POJ.2251 Dungeon Master (三维BFS)
POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...
- BFS POJ 2251 Dungeon Master
题目传送门 /* BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 */ #include <cstdio> #includ ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
随机推荐
- http各类攻击及tcpcopy工具
1.专业的还得ixia.Spirent TestCenter等软硬件一体的 2.一般的使用软件的,安装在linux上使用 参考: 1.http://blog.csdn.net/wuzhimang/ar ...
- linux下crontab使用笔记
1. 安装 service crond status yum install vixie-cron yum install crontabs 2. 实例 每分钟 ...
- 【J2SE高速进阶】——多线程之synchronized
我和老婆去银行取钱 有一天,和老婆打了个赌.如今我的银行账号里共同拥有5000块钱.我们去银行同一时候取钱,看我俩能不能同一时候取出5000来....(PS:打赌的代价是:假设都能取出5000,那这1 ...
- 2016.10.17 yaml文件里的labels和Pod、RC、Service的对应关系
在看kubernetes的例子时,出现了一个疑问. Pod.RC.Service的yaml文件里,都出现了labels,还有labelSelector.有些不太清楚,因此就这点来学习下. 接上文: ...
- POJ 2253 Frogger(最小最大距离)
题意 给你n个点的坐标 求第1个点到第2个点的全部路径中两点间最大距离的最小值 非常水的floyd咯 #include<cstdio> #include<cmath> #i ...
- Chrome自带恐龙小游戏的源码研究(完)
在上一篇<Chrome自带恐龙小游戏的源码研究(七)>中研究了恐龙与障碍物的碰撞检测,这一篇主要研究组成游戏的其它要素. 游戏分数记录 如图所示,分数及最高分记录显示在游戏界面的右上角,每 ...
- 常用js特效
事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.sr ...
- 计算两个GPS坐标点的距离
计算两个GPS坐标点的距离,第一个参数是第一个点的维度,第二个参数是第一个点的经度 http://yuninglovekefan.blog.sohu.com/235655696.html /** * ...
- java和erlang之间的DES加解密
app登录,登录的密码要用DES加密,服务器是用erlang,客户端要同时支持多平台(Android.iOS).首先,Java端的DES加密的实现方式, 少说废话了,直接上代码,如下: public ...
- PowerBuilder -- 未公开函数
原文:http://blog.csdn.net/happymagic/article/details/51077322 @.已知一个DW中的某列的列名(在字符串变量中),以获得这个列对象的DWO 方法 ...