poj 2251 Dungeon Master-搜索进阶-暑假集训

 普及一下知识

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
#include
#include
#include
#include//队列头文件
#include//栈头文件
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()
{
queueque;//这是声明存储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
{
for(j=0; j
{
getchar();//这里的getchar是吸收上一行迷宫后面的回车
for(k=0; 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;
}

 

转载于:https://www.cnblogs.com/wazqWAZQ1/p/4650177.html

你可能感兴趣的:(poj 2251 Dungeon Master-搜索进阶-暑假集训)