[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 ...
随机推荐
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- ABAP 关键字(1)
1.定义DATA ,TYPES TYPES关键字用于创建自定义数据类型,就像JAVA里面创建类一样,用TYPES创建的数据类型可以被其它变量引用(类似于实例化对象),而本身不能直接引用或者赋值. DA ...
- [容易] A + B 问题
题目来源:http://www.lintcode.com/zh-cn/problem/a-b-problem/
- Redis shell
Redis shell 命令 参数 功能 redis-cli -r 将一个命令执行多次 -i 每隔几秒执行一次 -x 和|一起接收前面地输出,并执行命令 -c -a --scan/--patt ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- history显示历史操作记录,并显示操作时间
在查看历史的操作记录有两种方式1.在用户的目录下的.bash_history文件中[root@node1 ~]# vi ~/.bash_history rebootvi /etc/sysconfig/ ...
- ruby 字符串
字符串处理函数 1.返回字符串的长度 str.length => integer 2.判断字符串中是否包含另一个串 str.include? other_str => true or fa ...
- ZOJ - 4016 Mergeable Stack 【LIST】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...
- haproxy 修改 访问路径
# 匹配 jsessionid,并去除 jessionid参数reqrep ^([^\ :]*)\ /a/test.html;jsessionid=.*\?(.*) \1\ /b/test.html? ...
- listen 73
Give Time to Feel Less Time-Squeeze Meetings, calls, kids, dogs, errands, exercise—and all those ema ...