UVA 532- Dungeon Master(三维bfs)

Dungeon Master

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

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 Specification

The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integers LR 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 LR and C.

Output Specification

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的最小步数。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>

using namespace std;
struct node
{
    int x,y,z;
    int step;
};
int k,n,m;
int jx[]={0,1,-1,0,0,0};
int jy[]={1,0,0,-1,0,0};
int jz[]={0,0,0,0,1,-1};
char map[110][110][110];
int vis[110][110][110];

void bfs(int s1,int s2,int s3,int t1,int t2,int t3)
{
    int i;
    struct node t,f;
    memset(vis,0,sizeof(vis));
    queue<node >q;
    t.x=s1;t.y=s2;t.z=s3;t.step=0;
    vis[t.z][t.x][t.y]=1;
    q.push(t);
    while(!q.empty()){
        t=q.front();
        q.pop();
        if(t.x==t1&&t.y==t2&&t.z==t3){
             printf("Escaped in %d minute(s).\n",t.step);
             return ;
        }
        for(i=0;i<6;i++){
            f.x=t.x+jx[i];
            f.y=t.y+jy[i];
            f.z=t.z+jz[i];
            if(f.z>=0&&f.z<k&&f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&!vis[f.z][f.x][f.y]&&map[f.z][f.x][f.y]!='#'){
                f.step=t.step+1;
                q.push(f);
                vis[f.z][f.x][f.y]=1;
            }
        }
    }
    printf("Trapped!\n");
}

int main()
{
    int i,j,l;
    int s1,s2,s3,t1,t2,t3;
    while(~scanf("%d %d %d",&k,&n,&m)){
        if(k==0&&n==0&&m==0) break;
        s1=s2=s3=0;
        t1=t2=t3=0;
        for(i=0;i<k;i++){
            for(j=0;j<n;j++){
                scanf("%s",map[i][j]);
                for(l=0;l<m;l++){
                    if(map[i][j][l]=='S'){
                        s1=j;
                        s2=l;
                        s3=i;
                    }
                    else if(map[i][j][l]=='E'){
                        t1=j;
                        t2=l;
                        t3=i;
                    }
                }
            }
        }
        bfs(s1,s2,s3,t1,t2,t3);
    }
    return 0;
}
时间: 2024-10-24 08:01:52

UVA 532- Dungeon Master(三维bfs)的相关文章

uva 532 Dungeon Master(BFS)

uva 532 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. Y

UVA 532 Dungeon Master

题目如下: Dungeon Master  You are trapped in a 3D dungeon and need to find the quickest way out!The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minuteto move one unit north,south, east, west, up or down. You ca

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

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

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 quicke

POJ 2251:Dungeon Master【bfs】

Dungeon Master Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 9   Accepted Submission(s) : 7 Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge