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 ...
随机推荐
- 转:Selenium-Grid工作方式
Selenium-Grid版本 selenium-grid分为版本1和版本2,其实它的2个版本并不是和selenium的版本1和2相对应发布的[即selenium-grid2的发布比selenium2 ...
- Android Studio的使用(十一)--每次打开时选择项目,而不是直接进入上次项目
1.打开的时候选择打开哪一个项目 2.需要在设置System Setting,不要勾选Reopen last project on startup项.
- Centos下安装jdk详解
环境: 系统: [root@Wulaoer ~]# cat /proc/version Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys ...
- 第11章 类的高级特性--final
1.final变量 (1)final关键字可用于变量声明,一旦该变量被设定,就不可以再改变该变量的值.通常,由final定义的变量为常量.例如:final double PI=3.14; final关 ...
- input text设置字体
控件里设置: style="font-family:Arial" html里设置 <font face="Arial">
- IPSec VPN实验
IPSec VPN实验 实验拓扑: 实验目的:掌握IPSec VPN原理 掌握site-to-site VPN配置 IPSec配置参数: IKE policy isakmp key 转换集 加密算法 ...
- svg都快忘了,复习一下
http://www.360doc.com/content/07/0906/21/39836_724430.shtml
- Android---->FrameLayout布局方式
main.xml布局代码分析-->实现一个渐变图案 <?xml version="1.0" encoding="utf-8"?> <Fr ...
- hrbust 1721 A + B = 0 map的应用
13级春季校赛的热身题,但优化后我的代码也超时了,后来看了看学长的解法,觉得最简单的还是map,再一次感受到了map的强大. 题目描述如下 Description There is an intege ...
- Python3基础 list() 将一个字符串转换成列表
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...