Dungeon Master (简单BFS)
Problem Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
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,在普通的二维平面图上增加了层数这个维度,除了在同一层东南西北方向上行走之外,还可以在相邻层之间行走,例如:可以从(x,y,0)---->(x,y,1),读懂题意后就很好写了,BFS模板走起
#include <algorithm>
#include <bitset>
//#include <bits/extc++.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue> using namespace std;
//using namespace __gnu_pbds #define ll long long
#define maxn 105 char maps[][][];
int dir[][] = {{, , }, {-, , }, {, , }, {, -, }, {, , }, {, , -}}, step[][][];
bool vis[][][];
int L, R, C; struct Node
{
int l, r, c;
} start, end1; bool check(Node x)
{
if (x.l < || x.l >= L || x.r < || x.r >= R || x.c < || x.c >= C || !vis[x.l][x.r][x.c] || maps[x.l][x.r][x.c] == '#')
{
return false;
}
return true;
} void bfs()
{
queue<Node> q;
q.push(start);
step[start.l][start.r][start.c] = ;
vis[start.l][start.r][start.c] = false;
while (!q.empty())
{
Node now = q.front();
q.pop();
for (int i = ; i < ; ++i)
{
Node next = now;
next.l += dir[i][];
next.r += dir[i][];
next.c += dir[i][];
// cout << next.l << " " << next.r << " " << next.c << endl;
// cout << maps[next.l][next.r][next.c] << endl;
if (check(next))
{
//cout << next.l << " " << next.r << " " << next.c << endl;
step[next.l][next.r][next.c] = step[now.l][now.r][now.c] + ;
vis[next.l][next.r][next.c] = false;
q.push(next);
}
}
}
} int main()
{
while (scanf("%d%d%d", &L, &R, &C) && L && R && C)
{
memset(step, -, sizeof(step));
memset(vis, true, sizeof(vis));
for (int i = ; i < L; ++i)
{
for (int j = ; j < R; ++j)
{
for (int k = ; k < C; ++k)
{
scanf(" %c", &maps[i][j][k]);
if (maps[i][j][k] == 'S')
{
start.l = i;
start.r = j;
start.c = k;
}
else if (maps[i][j][k] == 'E')
{
end1.l = i;
end1.r = j;
end1.c = k;
}
}
}
}
bfs();
if (step[end1.l][end1.r][end1.c] != -)
{
printf("Escaped in %d minute(s).\n", step[end1.l][end1.r][end1.c]);
}
else
{
puts("Trapped!");
}
}
return ;
}
Dungeon Master (简单BFS)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- 棋盘问题(DFS)& Dungeon Master (BFS)
1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- (简单) POJ 2251 Dungeon Master,BFS。
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj 2251 Dungeon Master(bfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- 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 ...
随机推荐
- springmvc整合freemarker教程(转)
1.介绍 我最近喜欢freemarker在网上找了大半天.都没有找到一个简单又容易理解的案例.虽然只是一个模板技术.但是相对刚开始什么都不知道的,很难入手.下面是自学(其实是谷歌和百度的东找西补).写 ...
- Delphi XE里的StrPas要注意哦(要让StrPas知道哪里是字符串结束)
废话不多说了,直接上例子解说: procedure TForm1.Button1Click(Sender: TObject);var aa: array[0..1]of AnsiChar; bb1 ...
- 第二阶段:2.商业需求分析及BRD:7.商业需求文档3
BRD模版 阐述需求来源以及调研分析情况 百度指数工具.定量的数据.发展趋势,是否与公司的战略冲突.环境政策:比如做内容的运营. 决策层看重的! 第二大块. 通过什么方式解决这个需求. 规划能力.类似 ...
- C++引用计数设计与分析(解决垃圾回收问题)
1.引言 上一篇博文讲到https://www.cnblogs.com/zhaoyixiang/p/12116203.html 我们了解到我们在浅拷贝时对带指针的对象进行拷贝会出现内存泄漏,那C++是 ...
- ArrayList数组扩容方式(基于jdk1.8)
ArrayList无参构造函数为: public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } 而DEFA ...
- pyspider遇到的第一个坑:Active Tasks成功,Results无内容
#!/usr/bin/env python# -*- encoding: utf-8 -*-# Created on 2020-01-04 16:30:27# Project: HomeWork fr ...
- 获取出口ip or api获取请求者ip
艾玛,这两天为了整这个ip 真的可谓无所不用其极. 在网上查阅了各种资料,其实我想实现的功能很简单 就像百度 直接看到自己的出口ip 奈何查了许多资料,都没有适合的解决办法. 灵机一动,我是不是可以访 ...
- 分支结构,for循环,while循环,跳出循环
#流程控制 概念:通过规定的语句让程序代码有条件的按照一定的方 式执行 顺序结构 按照书写顺序来执行,是程序中最基本的流程结构 选择结构(分支结构.条件结构) 分支结构 单路分支:if(执行的条件){ ...
- Time、Date拼接成TimeStamp
Time.Date拼接成TimeStamp 有关于Time类型.Date类型的数据这里不再赘述,本文旨在讲解如何将数据库中的Time.Date类型取出来并转换成TimeStamp类型,话不多说,先看代 ...
- 【转】C#中protected用法详解
https://www.cnblogs.com/wangyt223/archive/2012/08/08/2627801.html 在c#的可访问性级别中,public和private算是最容易理解的 ...