POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 2251

Appoint description: 
System Crawler  (2015-03-28)

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!

题目大意:

  给你一个3维的迷宫,维数是L*R*C,告诉了你起始的地点和终止的地点,每次只能够向6个方向来走动,问是否能够走出去,如果能的话,求出这个

最小的时间。

解题思路:

  直接BFS上最短时间问题,len[rear] = len[front]+1;每次进队前都要更新路径就好了。

代码:

# include<cstdio>
# include<iostream>
# include<algorithm>
# include<functional>
# include<cstring>
# include<string>
# include<cstdlib>
# include<iomanip>
# include<numeric>
# include<cctype>
# include<cmath>
# include<ctime>
# include<queue>
# include<stack>
# include<list>
# include<set>
# include<map>

using namespace std;

const double PI=4.0*atan(1.0);

typedef long long LL;
typedef unsigned long long ULL;

# define inf 999999999
# define MAX 33

char grid[MAX][MAX][MAX];
int book[MAX][MAX][MAX];
int len[MAX*MAX*MAX];
int l,r,c;
int ans;
int st_x,st_y,st_z;
int ed_x,ed_y,ed_z;

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

struct node
{
    int x;
    int y;
    int z;
}que[MAX*MAX*MAX];

void input()
{
        for ( int i = 0;i < l;i++ )
        {
            for ( int j = 0;j < r;j++ )
            {
                for ( int k = 0;k < c;k++ )
                {
                    char ch;
                    ch = getchar();
                    grid[i][j][k] = ch;
                    if ( ch==‘S‘ )
                    {
                        st_x = i;
                        st_y = j;
                        st_z = k;
                    }
                }
                getchar();
            }
            getchar();
        }
}

int bfs()
{
    int front,rear;
    memset(book,0,sizeof(book));
    memset(len,0,sizeof(len));
    que[0].x = st_x;
    que[0].y = st_y;
    que[0].z = st_z;
    rear = front = 0;
    while ( front <= rear )
    {
        for ( int i = 0;i < 6;i++ )
        {
            int n_x = que[front].x+next[i][0];
            int n_y = que[front].y+next[i][1];
            int n_z = que[front].z+next[i][2];

            if ( book[n_x][n_y][n_z]==0&&(grid[n_x][n_y][n_z]==‘.‘||grid[n_x][n_y][n_z]==‘E‘)&&n_x>=0&&n_x<l&&n_y>=0&&n_y<r&&n_z>=0&&n_z<c )
            {
                book[n_x][n_y][n_z] = 1;
                que[++rear].x = n_x;
                que[rear].y = n_y;
                que[rear].z = n_z;
                len[rear] = len[front]+1;
                if ( grid[n_x][n_y][n_z] == ‘E‘ )
                {
                    return len[rear];
                }
            }

        }
        front++;
    }
    return 0;

}

int main(void)
{
    while ( cin>>l>>r>>c )
    {
        if ( l==0&&r==0&&c==0 )
            break;
            getchar();
        input();
        ans = bfs();
        if ( ans )
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }

    return 0;
}

时间: 2024-10-01 07:42:06

POJ 2251 Dungeon Master(三维6方向BFS)的相关文章

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 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 wit

poj 2251 Dungeon Master(三维BFS)(中等)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20598   Accepted: 7971 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

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ - 2251 Dungeon Master(三维BFS)

题目链接:http://poj.org/problem?id=2251 题意:三维BFS. 题解:大水题,只不过多加了两个方向 1 //poj2251 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int sx,sy,sz,ex,ey,ez,L,R,C; 8 const int INF=

POJ 2251 Dungeon Master【三维BFS模板】

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted: 17256 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 wi

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 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

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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