转载请注明出处: 作者: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 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!

Dungeon Master (POJ - 2251)(BFS)的更多相关文章

  1. Dungeon Master(poj 2251)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  2. poj1753(位运算压缩状态+bfs)

    题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...

  3. HDU 4845 拯救大兵瑞恩(分层图状压BFS)

    拯救大兵瑞恩 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Sub ...

  4. [HNOI2006]最短母串问题(AC自动机+状态压缩+bfs)

    快要THUSC了,来水几道模板题吧. 这题其实是AC自动机模板.看到长度最短,首先就想到AC自动机.那么就直接暴力法来吧,把每个串建立在AC自动机上,建立fail指针,然后由于n<=12,可以把 ...

  5. Hie with the Pie(POJ 3311状压dp)

    题意:披萨店给n个地方送披萨,已知各地方(包括披萨店)之间花费的时间,求送完所有地方并回到店花费的最小时间 分析:状态好确定dp[i][j],i中1表示地方已送过,否则为0,j为当前状态最后一个送过的 ...

  6. Yogurt factory(POJ 2393 贪心 or DP)

    Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 4197 De ...

  7. Aizu 0531 "Paint Color" (坐标离散化+DFS or BFS)

    传送门 题目描述: 为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌. 三合板上不需要涂色的部分预先贴好了护板. 被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色. 请编写一个程序计 ...

  8. hdu4435 charge-station(先建后拆+bfs)

    charge-station Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. UVA 1599 Ideal Path(bfs1+bfs2,双向bfs)

    给一个n个点m条边(<=n<=,<=m<=)的无向图,每条边上都涂有一种颜色.求从结点1到结点n的一条路径,使得经过的边数尽量少,在此前提下,经过边的颜色序列的字典序最小.一对 ...

随机推荐

  1. THUWC2020滚粗记

    \(Day-?\) 教练叫走了3个人,没叫我 感觉药丸,然后被告知pku没过,thu过了 神奇,然后就活了 后来在机房颓废,大声说笑被diss 当时感觉颓的有点过头,药丸 \(Day0\) 跟NC去T ...

  2. Eclipse怎么改变@author 姓名

    1 点击windows  然后选择 点击进去选择搜索code Templates  点击选择出现下面的页面 点开comments,里面有给方法,变量 ,类等加注释设置的模板 如:点击Methods  ...

  3. git clone ssh 时出现 fatal: Could not read from remote repository

    一.问题及解决办法参考: 在 ubuntu 中,要把 GitHub 上的储存库克隆到计算机上时,执行如下命令: git clone git@github.com:USER-NAME/REPOSITOR ...

  4. ES6入门四:对象字面量扩展与字符串模板字面量

    简洁属性与简洁方法 计算属性名与[[prototype]] super对象(暂时保留解析) 模板字面量(模板字符串) 一.简洁属性与简洁方法 ES6中为了不断优化代码,减低代码的耦合度在语法上下了很大 ...

  5. 关于Webpack打包报错Class constructor FileManager cannot be invoked without 'new'

    前端代码部署一直是自己打包之后将文件用FileZilla上传到服务器上,现在改用运维基于到k8s docker镜像的发布,前端打包报错如下: 经查资料,报错原因是less升级导致的Bug 尝试升级le ...

  6. (转)JVM垃圾回收机制

    一.技术背景 GC的历史比Java久远,早在1960年Lisp这门语言中就使用了内存动态分配和垃圾回收技术 二.那些内存需要回收? JVM的内存结构包括五大区域:程序计数器.虚拟机栈.本地方法栈.堆区 ...

  7. mysql之使用json

    从mysql 5.7开始才有 创建表(含有json类型) CREATE TABLE `emp_details` ( `emp_no` int(11) NOT NULL, `details` json ...

  8. ansible 配置文件设置

    目录 ansible 配置文件设置 一.ansible configuration settings 二.ansible 配置文件查找顺序(从上到下,依次查找) 三.附录ansible配置参数 ans ...

  9. Delphi MSComm 控件方法

  10. gyp ERR! stack Error: EACCES: permission denied, mkdir问题解决方案

    sudo npm i --unsafe-perm 原因还是权限问题 就是说 npm 出于安全考虑不支持以 root 用户运行,即使你用 root 用户身份运行了,npm 会自动转成一个叫 nobody ...