POJ 2251 Dungeon Master BFS&DFS

  Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22227   Accepted: 8672

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.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!Source



以前在写DFS和BFS时没有系统的认识,做的东西也不足以真正拿出来,因为,我决定做一次算法突击,每天一到两道题, 这次这道题我就用DFS和BFS都写了一遍。发现两者的差异挺大,尤其是在时间复杂度上。DFS做这题直接是TLE,然后我换了BFS来做,还算是比较基础的BFS的题目,只不过是三维的。还有一个错误点,总是卡,就是for循环外面getchar(),后来换成scanf("%s",ma[i][j]);就对了。

代码:

#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>

using namespace std;

int a,b,c,sx,sy,sz,ex,ey,ez,vis[35][35][35];

int ax[6]= {0,0,0,0,1,-1},ay[6]= {0,0,1,-1,0,0},az[6]= {1,-1,0,0,0,0};

char ma[35][35][35];

struct point

{

    int x,y,z,step;

};

int judge(int x,int y,int z)

{

    if(x>=0&&y>=0&&z>=0&&x<a&&y<b&&z<c&&vis[x][y][z]==0&&ma[x][y][z]!='#')

        return 1;

    return 0;

}

void bfs()

{

    point star;

    point nex;

    memset(vis,0,sizeof(vis));

    queue<point> Q;

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

    star.x=sx;

    star.y=sy;

    star.z=sz;

    star.step=0;

    Q.push(star);

    while(!Q.empty())

    {

        star=Q.front();

        Q.pop();

        if(star.x==ex&&star.y==ey&&star.z==ez)

        {

            printf("Escaped in %d minute(s).\n",star.step);

            return;

        }

        for(int i=0; i<6; i++)

        {

            nex.x=star.x+ax[i];

            nex.y=star.y+ay[i];

            nex.z=star.z+az[i];

            if(judge(nex.x,nex.y,nex.z))

            {

                vis[nex.x][nex.y][nex.z]=1;

                nex.step=star.step+1;

                Q.push(nex);

            }

        }

    }

    printf("Trapped!\n");

    return;

}

int main()

{

    while(scanf("%d%d%d",&a,&b,&c)&&a)

    {

        getchar();

        for(int i=0; i<a; i++)

        {

            for(int j=0; j<b; j++)

            {

                scanf("%s",ma[i][j]);

                for(int k=0; k<c; k++)

                {

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

                    {

                        sx=i;

                        sy=j;

                        sz=k;

                    }

                    if(ma[i][j][k]=='E')

                    {

                        ex=i;

                        ey=j;

                        ez=k;

                    }

                }

            }

        }

        bfs();

    }

    return 0;

}

/*

3 4 5

S....

.###.

.##..

###.#

#####

#####

##.##

##...

#####

#####

#.###

####E


1 3 3

S##

#E#

###


0 0 0
*/ 



你可能感兴趣的:(基础,DFS,bfs,广搜,深搜)