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的一条路径,使得经过的边数尽量少,在此前提下,经过边的颜色序列的字典序最小.一对 ...
随机推荐
- C# 文件过滤器
首先说明一个示例,分析一下Filter属性的构成:“ Excel文件|*.xls ”,前面的“Excel文件”成为标签,是一个可读的字符串,可以自定定义,“|*.xls”是筛选器,表示筛选文件夹中后缀 ...
- 百度URL链接中文转码
百度搜索链接规则为: http://www.baidu.com/s?wd=[搜索词目]&cl=3 有多个搜索词通过加号进行链接: http://www.baidu.com/s?wd=keywo ...
- Anaconda--机器学习环境搭建
使用Anaconda为机器学习和深度学习设置Python环境 一.安装Anaconda,并更新到最新版本 1.Anaconda 安装: 官方网站下载地址 https://www.anaconda.co ...
- netty的断线重连问题
手里的这个项目需要作为客户端,不断的接收服务端发来的数据,用的netty框架,但是一直存在一个问题,就是断线重连问题. 什么是断线重连呢? 就是我们这个客户端要保证一直与服务端保持连接,这样客户端才能 ...
- shell脚本视频学习2
一.函数 1.函数格式 2.函数传入参数 3.手动输入函数中的参数 4.函数返回值 成功返回0,失败返回1 5.输入一个目录,判断目录是否存在,如果不存在则给出提示,如果存在则提示输入要创建的文件名, ...
- busybox date 时间的加减
1.下载安装busybox: # wget http://busybox.net/downloads/busybox-1.29.3.tar.bz2 # tar -jxvf busybox-.tar.b ...
- 九,configMap及secret的基本使用
目录 制定容器配置的方式 configMap(存储数据为明文,敏感数据慎用) 创建configMap的几种方式 命令行创建和测试configMap实例 创建一个Pod 挂载测试 通过指定文件创建con ...
- 4.Nginx配置文件Nginx.conf_虚拟主机配置规则
1.Nginx配置文件及各个配置项含义 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全 ...
- tp5将查询数据返回为对象转为数组
use think\Model; collection()->toArray(); $result = collection(model("Menu")->order( ...
- P_C_Brules
最小孔径10mil,最小线宽4mil,最小安全间距4mil.这个是一般厂家能做的.嘉立创为5mil. 1.xiankuan . 一般设为10mil.嘉立创多层板3.5mil,单双面5mil 电流的考量 ...