题目大意:给一个三维迷宫,给出入口和出口,找出最短路径。

  无权图上的单源最短路问题,使用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的更多相关文章

  1. 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 ...

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. poj 2251 Dungeon Master

    http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  4. Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏

    Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...

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

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

  6. UVa532 Dungeon Master 三维迷宫

        学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时)   #i ...

  7. Dungeon Master poj 2251 dfs

    Language: Default Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16855 ...

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

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

  9. BFS POJ2251 Dungeon Master

    B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. arrayList里的快速失败

    快速失败是指某个线程在迭代集合类的时候,不允许其他线程修改该集合类的内容,这样迭代器迭代出来的结果就会不准确. 比如用iterator迭代collection的时候,iterator就是另外起的一个线 ...

  2. PHP程序员学习路线

    注:本文是@黑夜路人的旧文,假设PHP程序员基础不是非常扎实,简单梳理了每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定学习成长目标.再次分享,共勉,欢迎补充. 第一阶段:基础阶段(基础 ...

  3. PHP中将ip地址转成十进制数的两种实用方法

    As we all know that long2ip works as ip1.ip2.ip3.ip4 (123.131.231.212) long ip => (ip1 * 256 * 25 ...

  4. jquery 延迟执行实例介绍

    代码如下: $(function(){ var $inputs = $('input[type=button]') .delay(500) .queue(function(){$(this).hide ...

  5. 剑指offer之有序二维数组查找

    大多数人注意到元素是行列有序的,会马上想到对每行(或列)进行二分查找,每行(或列)需要logN时间,N行(或列)共需要NlogN时间,很容易写出如下代码 1 2 3 4 5 6 7 8 9 10 11 ...

  6. dpkg -P xx

    dpkg -l | grep ^rc | cut -d' ' -f3|xargs dpkg -P http://www.linuxquestions.org/questions/debian-26/h ...

  7. Delphi天气预报查询

    Delphi天气预报查询 天气预报接口api(中国天气网) 开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供) 真正的中国天气api接口xml,json(求加精) ...

  8. 操作IFRAME及元素

    内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id="mainiframe..& ...

  9. 写插件时遇到的一个小问题,关于animate和css3的问题

    昨天写代码时,偶然想到了如果我们把css3属性放在animate中,指定时间.能否实现动画呢.举个例子吧: <script> $(".box").animate({ & ...

  10. 17.4.3 使用MulticastSocket实现多点广播(4)

    17.4.3  使用MulticastSocket实现多点广播(4) 通过UserInfo类的封装,所有客户端只需要维护该UserInfo类的列表,程序就可以实现广播.发送私聊信息等功能.本程序底层通 ...