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

最新文章

  1. POJ 2942 Knights of the Round Table
  2. 【Android】用HandlerThread模拟AsyncTask功能(ThreadTask)
  3. 内存泄漏检测工具Valgrind
  4. 一次MVVM+ReactiveCocoa实践
  5. 文本主题模型之LDA(一) LDA基础
  6. LinkedHashMap简明
  7. UVA1658:Admiral
  8. 从外面更新unity需要用的题库
  9. 精读《Scheduling in React》
  10. 小程序 navigator 无法跳转 tabBar上的页面
  11. FMS4.5( Adobe Flash Media Server4.5)流媒体服务器搭建
  12. 深度学习-Caffe编译测试的小总结
  13. 一个美国人对&quot;智能制造&quot;的思考!
  14. [Math]Pi(2)
  15. 【学习笔记】dp基础
  16. Mybatis之关联查询及动态SQL
  17. C 封装一个通用链表 和 一个简单字符串开发库
  18. 30个iPhone健康应用帮助你保持身体健康
  19. Linux内存管理机制简析
  20. mysql原理以及相关优化

热门文章

  1. X86逆向2:提取按钮通杀特征码
  2. 树莓派安装Firefox+Selenium+geckodriver
  3. 【原创】大叔经验分享(61)kudu rebalance报错
  4. VLC播放各种源
  5. 豆瓣网post 爬取带验证码
  6. java enum类自定义属性
  7. JS中数组与对象的遍历方法实例小结
  8. Python中文分词组件 jieba
  9. &amp; 位运算总结
  10. JVM学习笔记(一,待整理)