POJ2551Dungeon Master

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!

这道题是一道3D的搜索,可以用BFS,因为我最近正在练习BFS所以写一下,本来会的好多东西因为期末考试复习两周忘了好多,不过更好,正好从新学习,为蓝桥被准备!
不再墨迹了;
这题其实只是一道普通的搜索,不过我WA了几回后用自己的测试数据检验发现队列总是清不空,这一我选用的是用全局变量每次都重新定义队列,这样果然AC了;
所以我们有时候可以用队列时都重新定义,及把它放在BFS里;我觉得这是一个小技巧,所以发一下;代码如下:

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int dirx[6]={1,-1,0,0,0,0};
int diry[6]={0,0,1,-1,0,0};
int dirz[6]={0,0,0,0,1,-1};
char maps[31][31][31];
int vis[31][31][31];
int L,R,C;
struct point
{
int x;
int y;
int z;
int step;
};
point start,exit;
int bfs()
{
queue<point>que;
que.push(start);
point temp,cur;
memset(vis,-1,sizeof(vis));
vis[start.x][start.y][start.z]=0;

while(!que.empty())
{
temp=que.front();
// cout<<temp.step<<"HH"<<endl;
que.pop();
if(maps[temp.x][temp.y][temp.z]=='E')
{
return vis[temp.x][temp.y][temp.z];
break;
}

for(int i=0;i<6;i++)
{
cur.x=temp.x+dirx[i];
cur.y=temp.y+diry[i];
cur.z=temp.z+dirz[i];
if(cur.x>0&&cur.x<=L&&cur.y>0&&cur.y<=R&&cur.z>=0&&cur.z<=C&&maps[cur.x][cur.y][cur.z]!='#'&&vis[cur.x][cur.y][cur.z]==-1)
{
cur.step=temp.step+1;
que.push(cur);
vis[cur.x][cur.y][cur.z]=cur.step;
// cout<<cur.step<<endl;
}
}
}
return -1;
}
int main()
{


while(cin>>L>>R>>C)
{

if(L==0&&R==0&&C==0)
{
break;
}


for(int i=1;i<=L;i++)
{
for(int j=1;j<=R;j++)
{
for(int k=1;k<=C;k++)
{
cin>>maps[i][j][k];
if(maps[i][j][k]=='S')
{
start.x=i;
start.y=j;
start.z=k;
start.step=0;
}
}

}
}

// cout<<start.step<<"mm"<<endl;
int a=bfs();
if(a==-1)
{
cout<<"Trapped!"<<endl;
}
else
printf("Escaped in %d minute(s).\n",a);


}
}

你可能感兴趣的:(POJ2551Dungeon Master)