poj2251 Dungeon Master (三维迷宫) bfs广搜 简单题

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42890   Accepted: 16229

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!

三维迷宫,求从S到E的最短路径

做这个题之前,推荐先做二维迷宫问题

和二维迷宫一样的思路,只不过需要多增加一个维度z,然后搜索的方向由上下左右变成龙上下左右前后左右而已

思路一样

这道题最好别看代码,理解思路然后自己实现,毕竟由于是三维,所以写起来会比较难懂。

但是和二维的迷宫都是一样的做法,就不打注释了,想了解原理的话移步上面二维迷宫问题

#include 
#include 
#include 
#include 
#include 
#include 
#define For(i,n) for(int i=0;i Y;
char map[maxn][maxn][maxn];
const int move[6][3] = {{0,1,0},{0,-1,0},{-1,0,0},{1,0,0},{0,0,1},{0,0,-1}};
void Type(int a)
{
    printf("Escaped in %d minute(s).\n",a);
    ans = true;
}
void bfs(int x,int y,int z,int step)
{
    if(!Y.empty())
        Y.pop();
    if(map[z][y][x]!='#'&&map[z][y][x]!='E')
    {
        map[z][y][x] = '#';
        For(i,6)
        {
            if(x+move[i][0]=0)
                if(y+move[i][1]=0)
                    if(z+move[i][2]=0)
                        if(map[z+move[i][2]][y+move[i][1]][x+move[i][0]]!='#')
                        {
                            Pair buf;
                            buf.x = x+move[i][0];
                            buf.y = y+move[i][1];
                            buf.z = z+move[i][2];
                            buf.step = step+1;
                            Y.push(buf);
                        }
        }
    }
    if(map[z][y][x]!='E')
    {
        if(!Y.empty())
        {
            bfs(Y.front().x,Y.front().y,Y.front().z,Y.front().step);
        }
    }
    else
        Type(step);
}
int main()
{
    Pair s1;
    while(cin >> c >> b >> a&&a&&b&&c)
    {
        while(!Y.empty())
            Y.pop();
        cnt = 0;
        ans = false;
        mem(map,0);
        For(i,c)
        {
            For(j,b)
            {
                For(k,a)
                {
                    cin >> map[i][j][k];
                    if(map[i][j][k]=='S')
                    {
                        s1.x = k;
                        s1.y = j;
                        s1.z = i;
                    }
                }
            }
        }
        bfs(s1.x,s1.y,s1.z,0);
        if(!ans)
            cout<<"Trapped!"<


你可能感兴趣的:(搜索)