POJ 2251 Dungeon Master bfs

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24602   Accepted: 9525

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!

Source

Ulm Local 1997


3d bfs//原来不知道哪里错了盯着代码debug半天没结果 重新手敲一遍就a了- -

ACcode:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define maxn 33
using namespace std;
char mapp[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int to[][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
int n,m,l;
struct N{
    int x,y,z,t;
};
N my,no,ne;
bool ju(N a){
    if(a.x<0||a.x>=n||a.y<0||a.y>=m||a.z<0||a.z>=l||mapp[a.x][a.y][a.z]=='#'||vis[a.x][a.y][a.z])return 0;
    return 1;
}
void bfs(){
    memset(vis,false,sizeof(vis));
    queue<N>q;
    q.push(my);
    vis[my.x][my.y][my.z]=1;
    while(!q.empty()){
        no=q.front();q.pop();
        for(int i=0;i<6;++i){
            ne=no;
            ne.x+=to[i][0];
            ne.y+=to[i][1];
            ne.z+=to[i][2];
            ne.t++;
            if(ju(ne)){
                if(mapp[ne.x][ne.y][ne.z]=='E'){
                     printf("Escaped in %d minute(s).\n",ne.t);
                     return;
                }
                q.push(ne);
                vis[ne.x][ne.y][ne.z]=1;
            }
        }
    }
    printf("Trapped!\n");
}
int main(){
    while(~scanf("%d%d%d",&n,&m,&l)&&(n+m+l)){
        for(int i=0;i<n;++i)
            for(int j=0;j<m;++j)
                for(int z=0;z<l;++z){
                    cin>>mapp[i][j][z];
                    if(mapp[i][j][z]=='S'){
                        my.x=i;
                        my.y=j;
                        my.z=z;
                        my.t=0;
                    }
            }
        bfs();
    }
}


你可能感兴趣的:(C++,poj,bfs)