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: ...
随机推荐
- DEDECMS图片集上传图片出错302的解决办法
无忧主机(www.51php.com)小编今天在调试dede网站的时候发现了一个问题,因为小编想在网站上增加一个图片集的栏目,于是就到后台图片集栏目去添加内容,谁知在上传图片的时候给我弹出个错误信息框 ...
- IDEA使用Maven打包时如何去掉测试阶段
如图
- 2016.7.14 如何在浏览器中查看jsp文件
参考资料: http://jingyan.baidu.com/article/ed15cb1b10f1241be36981ab.html 1.复制jsp文件地址 2.写在浏览器里 E:/lyh/tas ...
- 入门--JTBC系统学习(1)
下载代码 JTBC有还例如以下几类 JDK(1.6)+JSP(2.0)+MYSQL/SQLITE ASP.NET(2.0)+ACCESS/SQL SERVER PHP+MYSQL ASP(3.0)+A ...
- 【Python】使用scatter()绘制散点图
绘制简单散点图 要绘制单个点,使用scatter()函数,并向它传递一对x和y坐标,它将在指定位置绘制一个点 import matplotlib.pyplot as plt plt.scatter(2 ...
- jquery 创建jquery的dom对象---------------获取自身的html节点及其子节点的html
1.var domObj = $("<dom>"); 2.var a = $("<a href='www.baidu.com'>"); ...
- 使用Hadoop自己的类操作HDFS
package hdfs; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.I ...
- java 中 instanceof 和 isInstance区别
两者的功能是等价的.区别: 1.instanceof 是一个操作符(类似new, ==等): 使用方法: if (ins instanceof String) { //logic } 2.isInst ...
- sql查字符串包含某字段查询
select * from dbo.V_AgreementMaterialQuery where '上海市' like '%'+SaleRange+'%' ‘上海市’>SaleRange(上海)
- Theme.AppCompat.Light.DarkActionBar ActionBarActivity
关于android-support-v7-appcompat.jar的引用.这个不单纯的把jar复制到项目lib目录下的,不然就会报一堆主题找不到的2b问题, 正确方法例如以下: 1.找到androi ...