[poj] Dungeon Master bfs
Description
Is an escape possible? If yes, how long will it take?
Input
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped! 三维的bfs, 总共可以向6个方向走,走一步步数+1, 可以按行读取地图, 把z轴设为最高维度, 基本过程和二维的bfs类似
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std; char maze[][][];
bool v[][][];
int l, r, c; int dir[][] = {{,,},{-,,},{,,},{,-,},{,,},{,,-}}; struct node
{
int x, y, z, t;
}now, Next;
int bx, by, bz;
int ex, ey, ez; int bfs()
{
now.x = bx;
now.y = by;
now.z = bz;
now.t = ;
v[bz][bx][by] = ;
queue<node> q;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.z == ez && now.x == ex && now.y == ey)
return now.t;
for (int i = ; i < ; i++) {
Next.z = now.z + dir[i][];
Next.x = now.x + dir[i][];
Next.y = now.y + dir[i][];
Next.t = now.t + ;
if (Next.x>=&&Next.x<r&&Next.y>=&&Next.y<c&&Next.z>=&&Next.z<l
&& !v[Next.z][Next.x][Next.y] && maze[Next.z][Next.x][Next.y] != '#')
{
q.push(Next);
v[Next.z][Next.x][Next.y] = ;
}
}
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
while (~scanf("%d%d%d", &l, &r, &c)) {
if (l+c+r == ) break;
memset(v, , sizeof(v));
for (int i = ; i < l; i++)
for (int j = ; j < r; j++)
scanf("%s", maze[i][j]); for (int i = ; i < l; i++)
for (int j = ; j < r; j++)
for (int k = ; k < c; k++) {
if (maze[i][j][k] == 'S') {
bz = i; bx = j; by = k;
}
if (maze[i][j][k] == 'E') {
ez = i; ex = j; ey = k;
} }
int ans = bfs();
if (ans == )
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", ans);
} return ;
}
[poj] Dungeon Master bfs的更多相关文章
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- hdu 2251 Dungeon Master bfs
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17555 Accepted: 6835 D ...
- poj 2251 Dungeon Master( bfs )
题目:http://poj.org/problem?id=2251 简单三维 bfs不解释, 1A, 上代码 #include <iostream> #include<cst ...
- poj 2251 Dungeon Master (BFS 三维)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
- Dungeon Master bfs
time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u POJ 2251 Descriptio ...
- POJ2251 Dungeon Master(bfs)
题目链接. 题目大意: 三维迷宫,搜索从s到e的最小步骤数. 分析: #include <iostream> #include <cstdio> #include <cs ...
随机推荐
- When Programmers and Testers Collaborate
When Programmers and Testers Collaborate Janet Gregory SOMETHING MAGICAL HAPPENS when testers and pr ...
- QQ登录集成到自己网站php代码(转载)
我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考. 1.打开open.qq.com 添加创 ...
- ABAP 设置单元格颜色
http://blog.163.com/ronanchen@126/blog/static/172254750201161811040488/ http://blog.csdn.net/lhx20/a ...
- update module (更新模块)
[转自http://blog.csdn.net/zhongguomao/article/details/6712568] function module:更新程序必须用一个特殊的FM(update m ...
- RaspBerry Pi3 ~ 内核编译
RaspBerryPi3-内核编译 转载注明出处:http://www.cnblogs.com/einstein-2014731/p/5985128.html 在有道云笔记的同步分享:http://n ...
- Data Structure Binary Tree: Level order traversal in spiral form
http://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/ #include <iostream> #includ ...
- (C)结构数组
结构数组 对于大小相同但是类型不同的数组,定义结构体数组对其很有帮组.例如: char *keyword[NKEYS]; int keycount[NKEYS]; 这两个数组大小相同,因此 可以用另一 ...
- 和菜鸟一起学android4.0.3源码之硬件gps简单移植【转】
本文转载自:http://blog.csdn.net/mwj19890829/article/details/18751447 关于Android定位方式 android 定位一般有四种方法,这四种方 ...
- 分享知识-快乐自己:Linux—jdk 安装步骤
1.查看现有版本:java -version 2.查看jdk的具体版本: rpm -qa| grep jdk || rpm -qa| grep gcj 3.删除已安装jdk包: rpm -e --no ...
- malloc和new的区别是什么?
http://zhidao.baidu.com/link?url=iUDUZeJtj1o12PvUETLlJgvAMqzky5HxGCJRGnULpsO8HdWAdjKkQqGCJ9-o-aTu8NP ...