Dungeon Master (POJ - 2251)(BFS)
转载请注明出处: 作者:Mercury_Lc 地址:https://blog.csdn.net/Mercury_Lc/article/details/82693907
题解:三维的bfs,一开始不怎么理解,就找各种题解,首先要懂的在二维平面上的bfs,bfs一般用来求能够到达某一点使经过的图上的点的值尽可能的小或者是给你两个值x,y,问x能否经过x=2*x或者x+=1这两种操作来变成y(Catch That Cow)。
当然本题是第一种类型,不过除了在平面x0y上下左右行走之外,还可在z轴的方向上行走,这样只需要把那个控制方向的数组开成三维的就可以了,这里注意一点的是,三维数组的第一个是z,其他的地方和二维的类似,就没什么难点了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 60;
int L, R, C;
char gra[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
struct node
{
int x, y, z;
int step;
} S,E,w,l;
int dx[] = {0,0,0,0,1,-1};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {1,-1,0,0,0,0};
void bfs()
{
memset(vis,false,sizeof(vis));
vis[S.z][S.x][S.y] = true;
queue<node>q;
q.push(S);
while(!q.empty())
{
w = q.front();
q.pop();
if(w.x==E.x&&w.y==E.y&&w.z==E.z)
{
printf("Escaped in %d minute(s).\n", w.step);
return ;
}
for(int i = 0; i < 6; i ++)
{
l = w;
l.x += dx[i];
l.y += dy[i];
l.z += dz[i];
if(l.z>=0&&l.z<L&&l.x>=0&&l.x<R&&l.y>=0&&l.y<C&&gra[l.z][l.x][l.y]!='#'&& !vis[l.z][l.x][l.y])
{
l.step++;
q.push(l);
vis[l.z][l.x][l.y] = true;
}
}
}
printf("Trapped!\n");
}
int main()
{
while(~scanf("%d %d %d", &L, &R, &C)&&L &&R && C)
{
getchar();
for(int i =0; i < L; i ++)
{
for(int j =0; j < R; j++)
{
scanf("%s", gra[i][j]);
for(int w = 0; w < C; w ++)
{
if(gra[i][j][w] == 'S')
{
S.z = i;
S.x = j;
S.y = w;
S.step = 0;
}
else if(gra[i][j][w] == 'E')
{
E.z = i;
E.x = j;
E.y = w;
}
}
}
}
bfs();
}
return 0;
}
Problem
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
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
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
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
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 lineTrapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0Sample Output
Escaped in 11 minute(s).
Trapped!
Dungeon Master (POJ - 2251)(BFS)的更多相关文章
- Dungeon Master(poj 2251)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- poj1753(位运算压缩状态+bfs)
题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...
- HDU 4845 拯救大兵瑞恩(分层图状压BFS)
拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Sub ...
- [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)
快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...
- Hie with the Pie(POJ 3311状压dp)
题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间 分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的 ...
- Yogurt factory(POJ 2393 贪心 or DP)
Yogurt factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8205 Accepted: 4197 De ...
- Aizu 0531 "Paint Color" (坐标离散化+DFS or BFS)
传送门 题目描述: 为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌. 三合板上不需要涂色的部分预先贴好了护板. 被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色. 请编写一个程序计 ...
- hdu4435 charge-station(先建后拆+bfs)
charge-station Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- UVA 1599 Ideal Path(bfs1+bfs2,双向bfs)
给一个n个点m条边(<=n<=,<=m<=)的无向图,每条边上都涂有一种颜色.求从结点1到结点n的一条路径,使得经过的边数尽量少,在此前提下,经过边的颜色序列的字典序最小.一对 ...
随机推荐
- 如何修改maven本地仓库位置
1.创建文件夹D:\m2\repository 2.修改E:\apache-maven-3.5.4\conf\setting.xml文件.将 改成<localRepository>< ...
- WebMvcConfigurationSupport跨域和fastjson全局替换
@Configuration public class WarnWebMvcConfigurationSupport extends WebMvcConfigurationSupport { /** ...
- Java 注解(原理及其使用)
一.注解(annotation)介绍 Java在JDK5中引入源代码的注解机制. 1.什么是注解? 注解为代码添加了元数据,元数据是关于数据的组织.数据域及其关系的说明信息. 更通俗的说,注解为程序元 ...
- C# WPF 数据绑定
后台通知: public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(str ...
- python numpy 的用法—— bincount
今天看脚本的时候遇到了几个不懂的用法,记录下来供日后查看: 1.numpy bincount 先上图: 如上所示:首先要求输入的数组不能包含负数: 该函数是计算非负元素的个数,如果数组中的最大值为10 ...
- mark ubuntu 16.04 64bit + cpu only install mtcnn
大神代码链接 称之为MTCNN人脸检测算法,同时有大神已经GitHub上开源了其基于caffe的C++ API 的源代码,https://github.com/DaFuCoding/MTCNN_Caf ...
- kali入侵服务器之后清除痕迹
Linux清除痕迹 第一种方法: 在退出会话前直接执行: #history -r 清除当前会话的命令历史记录 第二种方法: 在vim中执行自己不想让别人看到的命令随便用vim打开一个文件 :set h ...
- 第十五章、Python多线程之信号量和GIL
目录 第十五章.Python多线程之信号量和GIL 1. 信号量(Semaphore) 2. GIL 说明: 第十五章.Python多线程之信号量和GIL 1. 信号量(Semaphore) 信号量用 ...
- 关于PXELINUX的一些重要描述摘录
以下资源都来自官方文档,原文摘录 PXELINUX is a SYSLINUX derivative, for booting Linux off a network server, using a ...
- /build-impl.xml:1030: The module has not been deployed.(netbean javaweb)
我在netbean上创建了一个javaweb,这个项目创建成功了,但是运行时却有了错误,错误贴图如下 报错: The module has not been deployed. See the se ...