BFS解决POJ 2251

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!

 
此题目是三维的迷宫问题,要想解决本题,首先要有二维的BFS相关知识,
有了这个之后,还是不行的,还要搞清楚行走的坐标的表示。
一开始,我本以为要开一个三维的数组来表示走法,但是,用二维的才是正确的。
如何正确,我将在代码中做出详细的说明。另外的一些注意事项,也会在代码中
做出详细的解释。
View Code
#include<iostream>

#include<string.h>

using namespace std;

int level, row, col, i, j, k, sx, sy, sz, tx, ty, tz;

char map[35][35][35];

int used[35][35][35];

int step[35][35][35];

int direction[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};

///好了,现在来好好解释一下direction[][]数组,也就是走法。一共有6种走法,上下左右前后。故要开direction[6][];

///为何要开direction[][3]呢?也就是这个3该如何解释呢?学过几何的人都知道x,y,z的空间坐标轴。这就是3的来历。

///拿{0,0,1}来说,表示x=0, y=0, z=1,很明显了,这种走法表示的是向上走了。其它的类似,不再说了。

///同样的,direction[4][2]中的2,也是同样的道理。只不过没有了z坐标。

struct point

{

    int x,y,z;

}queue[30000];

int BFS(int sz, int sx, int sy)

{

    used[sz][sx][sy] = 1;

    int head = 0, tail = 0;

    queue[head].z = sz;

    queue[head].x = sx;

    queue[head].y = sy;

    head++;

    while(tail<head)

    {

        point t1 = queue[tail];

        tail++;

        int m;

        for(m=0; m<6; m++)

        {

            point t2;

            t2.z = t1.z+direction[m][2];

            t2.x = t1.x+direction[m][0];

            t2.y = t1.y+direction[m][1];

            if(t2.z>=0 && t2.z<level && t2.x>=0 && t2.x<row && t2.y>=0 && t2.y<col && used[t2.z][t2.x][t2.y]==0)

            {

                if(map[t2.z][t2.x][t2.y]=='.')

                {

                    step[t2.z][t2.x][t2.y] = step[t1.z][t1.x][t1.y]+1;

                    used[t2.z][t2.x][t2.y] = 1;

                    queue[head] = t2;

                    head++;

                }

                else if(map[t2.z][t2.x][t2.y]=='E')

                    return (step[t1.z][t1.x][t1.y]+1);

            }

        }

    }

    return -1;

}

int main()

{

    while(cin>>level>>row>>col && (level+row+col))

    {

        memset(used, 0, sizeof(used));

        memset(step, 0, sizeof(step));

        for(i=0; i<level; i++)

            for(j=0; j<row; j++)

                for(k=0; k<col; k++)

                {

                     cin>>map[i][j][k];

                    if(map[i][j][k]=='S')

                      ///一定要注意这里的map[i][j][k]中的三个参数i, j, k.

                     /// i 对应层数,也就是z轴

               /// j 对应行数,也就是x轴

               /// z 对应列数,也就是y轴

               ///因此,在下面的运算中要一一对应起来的。否则将会出错的。

                    {

                        sx = j; sy = k; sz = i; 

                    }

                }

        int result = BFS(sz, sx, sy);

        if( result==-1 )

            cout<<"Trapped!"<<endl;

        else

            cout<<"Escaped in "<<result<<" minute(s)."<<endl;

    }

}
 
   

 

 
 
  

你可能感兴趣的:(poj)