普及一下知识

s.empty() 如果栈为空返回true,否则返回false
s.size() 返回栈中元素的个数
s.pop() 删除栈顶元素但不返回其值
s.top() 返回栈顶的元素,但不删除该元素
s.push() 在栈顶压入新元素

q.empty() 如果队列为空返回true,否则返回false
q.size() 返回队列中元素的个数
q.pop() 删除队列首元素但不返回其值
q.front() 返回队首元素的值,但不删除该元素
q.push() 在队尾压入新元素
q.back() 返回队列尾元素的值,但不删除该元素

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!

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>//队列头文件
#include<stack>//栈头文件
using namespace std;
#define N 35
int level, row, column, visit[N][N][N];//level代表水平方向, row代表行, column代表列,visit为标记数组
//这是他逃跑的六个方向
int dir[6][3]= {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};

char maps[N][N][N];

typedef struct maze//定义结构体,也就是他的坐标和现在用了多少时间
{
int l, r, c, t;
} MAZE;
MAZE n, m, s, e;

bool check(int x, int y, int z)//判断这个点是否符合要求,才可以入队列
{
if(x<0||x>=level||y<0||y>=row||z<0||z>=column||visit[x][y][z]==1||maps[x][y][z]=='#')
return false;
return true;
}

int BFS()
{
queue<MAZE>que;//这是声明存储MAZE类型数据的队列
que.push(s);//入队列

while(que.size())//替换成while(!que.empty())其实也可以
{
m=que.front();//是访问队列的第一个即最低端元素,
que.pop();//出队列,队列遵循先进先出,所以这里取出的是最低端的元素;
if(m.l==e.l&&m.r==e.r&&m.c==e.c)//广搜停止的条件
return m.t;
for(int i=0; i<6; i++)
{
n=m;
n.l+=dir[i][0];
n.r+=dir[i][1];
n.c+=dir[i][2];
n.t++;
if(check(n.l, n.r, n.c))
{
visit[n.l][n.r][n.c]=1;
que.push(n);
}
}
}
return -1;
}

int main()
{
int i, j, k, answer;
while(scanf("%d%d%d", &level, &row, &column), !(level==0&&row==0&&column==0))//这里也可以写成(level!=0||row!=0||column!=0)
{
memset(visit, 0, sizeof(visit));
for(i=0; i<level; i++)
{
for(j=0; j<row; j++)
{
getchar();//这里的getchar是吸收上一行迷宫后面的回车
for(k=0; k<column; k++)
{
scanf("%c", &maps[i][j][k]);
if(maps[i][j][k]=='S')
{
s.l=i;
s.r=j;
s.c=k;
s.t=0;
}
if(maps[i][j][k]=='E')
{
e.l=i;
e.r=j;
e.c=k;
}
}
}
getchar();//这道题level个矩阵后都又跟了一行回车或空格,这个getchar是吸收每个矩阵后面的回车;
}
answer=BFS();
if(answer==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", answer);
}
return 0;
}

最新文章

  1. 1002. A+B for Polynomials (25)
  2. 利用.net的内部机制在asp.net中实现身份验证
  3. 通过 ES6 Promise 和 jQuery Deferred 的异同学习 Promise
  4. freeCodeCamp:Find the Longest Word in a String
  5. mvc5入门示例博客(有惊喜)
  6. powershell: 生成随机字符串
  7. php获取从百度搜索进入网站的关键词
  8. Elasticsearch 权威指南 NESTAPI地址
  9. Java——(三)Collection之Set集合、HashSet类
  10. Qt使用QGraphicsView实现滑动窗体效果
  11. Qt4----子例化QDialog(可扩展对话框的使用)
  12. 关于PS的一些总结
  13. Java面经 面试经验 互联网公司面试经验 后端面试经验
  14. BZOJ 3879: SvT [虚树 后缀树]
  15. ubuntu14.04 64位 安装H3C iNode客户端
  16. IP Core 分类
  17. JavaScript中的内置对象-8--3.Math-Math对象的方法-min()- max()- ceil() - floor()- round()- abs(); Math对象的random()方法;
  18. vue router返回上一页
  19. ssm框架整合shiro
  20. Jungle Roads_hdu_1301(prim算法)

热门文章

  1. python 读写 json文件
  2. java.lang.UnsupportedOperationException解决方法!!!
  3. 篇章三:[AngularJS] 使用AngularCSS動態載入CSS
  4. eclipse编写scala应用运行在spark集群上
  5. Hbase和RDBMS(关系数据库管理系统)区别
  6. Junit内部解密之三: 单元测试用例运行的全过程
  7. [译]NeHe教程 - 创建一个OpenGL窗体
  8. Tautology - poj 3295
  9. 一次 read by other session 的处理过程
  10. 删除路径下的文件以及子路径。(范例“E:/www/”)