Dungeon Master(BFS)

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!

就是六个方向的广搜,理清楚就好做
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int a,b,c;
char s[30][30][30];
int map[30][30][30];
int next[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int si,sj,sk,ei,ej,ek;
struct node
{
    int x,y,z;
    int step;
};
int check(int x,int y,int z)
{
    if(x<0 || y<0 || z<0 || x>=a || y>=b || z>=c)
        return 1;
    else if(s[x][y][z] == ‘#‘)
        return 1;
    else if(map[x][y][z])
        return 1;
    return 0;
}
int bfs()
{
    queue <node> q;
    node t,no;
    t.x=si;t.y=sj;t.z=sk;
    t.step=0;
    map[si][sj][sk]=1;
    q.push(t);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        if(t.x == ei && t.y == ej && t.z == ek)
            return t.step;
        for(int i = 0; i<6; i++)
        {
            no = t;
            no.x = t.x+next[i][0];
            no.y = t.y+next[i][1];
            no.z = t.z+next[i][2];
            if(check(no.x,no.y,no.z))
                continue;
            map[no.x][no.y][no.z] = 1;
            no.step = t.step+1;
            q.push(no);
        }
    }
    return 0;
}
int main()
{
    while(cin>>a>>b>>c)
    {
        if(a==0&&b==0&&c==0)
            break;
        for(int i=0;i<a;i++)
        {
            for(int j=0;j<b;j++)
            {
                for(int k=0;k<c;k++)
                {
                    cin>>s[i][j][k];
                    if(s[i][j][k]==‘S‘)
                    {
                        si=i;
                        sj=j;
                        sk=k;
                    }
                    if(s[i][j][k]==‘E‘)
                    {
                        ei=i;
                        ej=j;
                        ek=k;
                    }
                }
            }
        }
        memset(map,0,sizeof(map));
        int ans=bfs();
        if(ans)
            cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
        else
            cout<<"Trapped!"<<endl;
    }
    return 0;
}

  

				
时间: 2024-10-24 08:53:29

Dungeon Master(BFS)的相关文章

hdu 2251 Dungeon Master bfs

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

[poj] Dungeon Master bfs

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 m

E - Dungeon Master BFS

[NWUACM] 你被困在一个三维的空间中,现在要寻找最短路径逃生!空间由立方体单位构成你每次向上下前后左右移动一个单位需要一分钟你不能对角线移动并且四周封闭是否存在逃出生天的可能性?如果存在,则需要多少时间? Input - 输入 输入第一行是一个数表示空间的数量. 每个空间的描述的第一行为L,R和C(皆不超过30). L表示空间的高度. R和C分别表示每层空间的行与列的大小. 随后L层地牢,每层R行,每行C个字符. 每个字符表示空间的一个单元.'#'表示不可通过单元,'.'表示空白单元.你的

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: 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

ZOJ 1940 Dungeon Master 三维BFS

Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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 r

POJ:Dungeon Master(三维bfs模板题)

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

[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master

B - Dungeon Master 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 c