poj2251——bfs

POJ 2251  bfs

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18245   Accepted: 7075

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!

题意:在三维空间上找最短路,其实就是bfs,和二维没有区别,水题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>

using namespace std;

const int maxn=50;
const int INF=(1<<28);

int X,Y,Z;
char ch[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int sx,sy,sz;
int ans;
int dx[]={-1,1,0,0,0,0};
int dy[]={0,0,-1,1,0,0};
int dz[]={0,0,0,0,-1,1};

struct node
{
    int x,y,z;
    int dist;
};

bool bfs()
{
    queue<node> q;
    q.push({sx,sy,sz,0});
    vis[sx][sy][sz]=1;
    while(!q.empty()){
        node now=q.front();
        q.pop();
        for(int i=0;i<6;i++){
            int nx=now.x+dx[i];
            int ny=now.y+dy[i];
            int nz=now.z+dz[i];
            int nd=now.dist+1;
            if(ch[nx][ny][nz]=='E'){
                ans=nd;
                return true;
            }
            if(ch[nx][ny][nz]=='#') continue;
            if(vis[nx][ny][nz]) continue;
            vis[nx][ny][nz]=1;
            q.push({nx,ny,nz,nd});
        }
    }
    return false;
}

int main()
{
    while(cin>>X>>Y>>Z,X||Y||Z){
        memset(ch,'#',sizeof(ch));
        for(int i=1;i<=X;i++){
            for(int j=1;j<=Y;j++){
                for(int k=1;k<=Z;k++){
                    cin>>ch[i][j][k];
                    if(ch[i][j][k]=='S'){
                        sx=i;
                        sy=j;
                        sz=k;
                    }
                }
            }
        }
        ans=0;
        memset(vis,0,sizeof(vis));
        if(bfs()) printf("Escaped in %d minute(s).\n",ans);
        else printf("Trapped!\n");
    }
    return 0;
}
poj2251_bfs

 

posted @ 2015-03-13 00:18  __560  阅读(264)  评论(0编辑  收藏  举报