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

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!

就是一个人被逮到地牢里面了,地牢有好几层(L),每层R*C,标#的地方不能走,每次可以上、下、左、右、前、后走一个相邻的格子,花费1分钟。问你从S走,能不能逃出这个地牢(走到E)。
这是我认真的写的第一个dfs,是不是有点晚呢?呵呵。总结一下dfs的几个要点吧。 1.struct node 用于标记点的坐标,和到达(如果能)该店的步数。
2.vis dir 数组,分别表示 是否访问过 每次走的方向
3.check函数 :检查3点 1->是否超出边界 2->是否走过 3->是否能走
4.基于队列实现的bfs函数,这里详细解释
首先我们建立一个node的队列q,我们先把起点加进队列。从当q的队首开始,用temp取得这个队首,q.pop().将通过temp能走到的node再加入到队列里面,同时判断是否到达终点。
这样知道q这个队列空了,我们就走完了所有可能的路了。 代码如下:
 #include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
int l,x,y;
int step;
};
int L,R,C;
int el,ex,ey,sl,sx,sy,ans;
char mp[][][];
bool vis[][][];
int dir[][]={,,, -,,, ,,, ,-,, ,,, ,,-};
bool check(node now)
{
if (now.l>=L||now.l<||
now.x>=R||now.x<||
now.y>C||now.y<)
return ;
if (mp[now.l][now.x][now.y]=='#'||vis[now.l][now.x][now.y])
return ;
return ;
}
void bfs (int level,int xx,int yy)
{
queue<node> q;
node now;
now.l=level,now.x=xx,now.y=yy,now.step=;
vis[level][xx][yy]=true;
q.push(now);
while (!q.empty())
{
node temp=q.front();
q.pop();
for (int i=;i<;++i)
{
now.l=temp.l+dir[i][];
now.x=temp.x+dir[i][];
now.y=temp.y+dir[i][];
now.step=temp.step+;
if (check(now))
{
if (mp[now.l][now.x][now.y]=='E')
{
ans=now.step;
return;
}
vis[now.l][now.x][now.y]=true;
q.push(now);
}
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d%d",&L,&R,&C))
{
if (L==&&R==&&C==)
break;
el=ex=ey=;
sl=sx=sy=;
memset(vis,false,sizeof vis);
for (int i=;i<L;++i)
{
for (int j=;j<R;++j)
{
for (int k=;k<C;++k)
{
cin>>mp[i][j][k];
if (mp[i][j][k]=='S')
{
sl=i,sx=j,sy=k;
}
if (mp[i][j][k]=='E')
{
el=i,ex=j,ey=k;
}
}
}
}
ans=-;
bfs(sl,sx,sy);
if (ans!=-)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return ;
}
 

最新文章

  1. C#语言基础
  2. 随便玩玩系列之一:SPOJ-RNG+51nod 算法马拉松17F+51nod 1034 骨牌覆盖v3
  3. 1472. Martian Army
  4. C++11新特性 lambda表达式
  5. 通过Unity3D制作天空盒
  6. jquery版悬浮模块demo
  7. asp.net关于页面不回发,不生成__doPostBack方法问题的完美解决方案
  8. PAT (Basic Level) Practise:1002. 写出这个数
  9. Oracle查询慢, 特别是更新慢问题
  10. iOS OC开发代码规范
  11. mysql5.7 二进制包安装
  12. 系统变量写在.bash_profile和.bashrc的区别
  13. 有关HTTP的粗读
  14. Python——模块——配置模块(ConfigParser)
  15. 阿里云服务器ECS的环境部署和安装
  16. redis&#160;redis常用命令及内存分析总结(附RedisClient工具简介
  17. 巧用cheerio重构grunt-inline
  18. 打开ahci模式
  19. N项阶乘累加求和新算法
  20. JetBrains激活 PyCharm | IntelliJ IDEA | CLion | WebStorm...

热门文章

  1. 01 spring security入门篇
  2. 阿里下一代云分析型数据库AnalyticDB入选Forrester云化数仓象限
  3. 因为信仰,油画专业的他自学开发进击阿里技术P9
  4. Python3解leetcode Maximum SubarrayHouse Robber
  5. [CSP-S模拟测试]:Lost My Music(凸包)
  6. 109、TensorFlow计算张量的值
  7. 如何消去delphi Stringgrid重绘时产生重影
  8. luoguP1314 聪明的质监员 题解(NOIP2011)
  9. 一些常ArcGIS常用简单算法 C#
  10. 数组归一 reduce (数组归一) reduceRight(从右至左)