UVa 532 - Dungeon Master
题目大意:给一个三维迷宫,给出入口和出口,找出最短路径。
无权图上的单源最短路问题,使用BFS解决。
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define MAXN 35 const int dir[][] = {{-, }, {, -}, {, }, {, }, {-, }};
int G[MAXN][MAXN][MAXN], dist[MAXN*MAXN*MAXN];
int L, R, C;
int s, e; int bfs()
{
queue<int> q;
memset(dist, -, sizeof(dist));
q.push(s);
dist[s] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
int l = u / (R*C);
int r = (u%(R*C)) / C;
int c = (u%(R*C)) % C;
for (int i = ; i < ; i++)
{
int rr = r + dir[i][];
int cc = c + dir[i][];
int v = l*(R*C) + rr*C + cc;
if (rr >= && rr < R && cc >= && cc < C && G[l][rr][cc] && dist[v] == -)
{
q.push(v);
dist[v] = dist[u] + ;
if (v == e) return dist[e];
}
}
for (int i = ; i < ; i++)
{
int ll = l + dir[][i];
int v = ll*(R*C) + r*C + c;
if (ll >= && ll < L && G[ll][r][c] && dist[v] == -)
{
q.push(v);
dist[v] = dist[u] + ;
if (v == e) return dist[e];
}
}
}
return dist[e];
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
char str[];
while (scanf("%d%d%d", &L, &R, &C) && (L || R || C))
{
getchar();
for (int k = ; k < L; k++)
{
for (int i = ; i < R; i++)
{
gets(str);
for (int j = ; j < C; j++)
{
if (str[j] == '.') G[k][i][j] = ;
else if (str[j] == '#') G[k][i][j] = ;
else if (str[j] == 'S')
{
s = k*(R*C) + i*C + j;
G[k][i][j] = ;
}
else if (str[j] == 'E')
{
e = k*(R*C) + i*C + j;
G[k][i][j] = ;
}
}
}
gets(str);
}
int ans = bfs();
if (ans > ) printf("Escaped in %d minute(s).\n", ans);
else if (ans == -) printf("Trapped!\n");
}
return ;
}
UVa 532 - Dungeon Master的更多相关文章
- 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 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- POJ 2251 Dungeon Master(地牢大师)
p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: ...
- BFS POJ2251 Dungeon Master
B - Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- AndroidGradle --多渠道打包配置(转发)
需求 国内Android app发布一般会有多个渠道,为了跟踪发展情况,通常会为每一个渠道定制一个特别的apk. 一般友盟之类第三方统计的渠道ID定义如下,以wandoujia为例 <meta- ...
- Android Studio的使用(十一)--每次打开时选择项目,而不是直接进入上次项目
1.打开的时候选择打开哪一个项目 2.需要在设置System Setting,不要勾选Reopen last project on startup项.
- ural1701 Ostap and Partners
Ostap and Partners Time limit: 2.0 secondMemory limit: 64 MB Workman Ivan lost his job. Not because ...
- UIView的alpha、hidden、opaque 深入
转载自:http://blog.csdn.net/wzzvictory/article/details/10076323 UIView的这几个属性让我困惑了好一阵子,通过翻看官方文档和stackove ...
- POJ 2991 Crane
线段树+计算几何,区间更新,区间求和,向量旋转. /* *********************************************** Author :Zhou Zhentao Ema ...
- JS检测图片的大小
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...
- paramiko 模块安装
windows版本: 所需软件有:PyCrypto.ecdsa.paramiko. 一.软件下载地址 1.PyCrypto下载地址: http://www.voidspace.org.uk/pyth ...
- gnome3 no launcher
http://askubuntu.com/questions/43246/how-to-configure-gnome-3-to-show-icons-on-desktop http://superu ...
- clearsSelectionOnViewWillAppear
@property (nonatomic) BOOL clearsSelectionOnViewWillAppear NS_AVAILABLE_IOS(3_2); // defaults to YES ...
- Scratch2.0例—接苹果
Scratch2.0例—接苹果 [教学目标] 1. 学习例子,能用和构造条件 ,并把此条件插入到 中:能理解和应用,当条件成立时,不执行积木内的脚本. 2. 对比和 两个积木:前者用于无条件的重复执行 ...