Loading

POJ2251 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 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 <cmath>
#include <algorithm>
#include <vector> 
#include <cstring>
#include <queue>
using namespace std;
int L,R,C;
char mmap[35][35][35];
bool vis[35][35][35]={0};
int dir[6][3]={{-1,0,0},{1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//x y z
struct point 
{
    int x;
    int y;
    int z;
    int cnt;
};
point start,end;
void bfs()//cnt统计步数 
{
   memset(vis,0,sizeof(vis));
  queue<point>q;
   point pre,nxt;
   vis[start.z][start.x][start.y]=1;
   start.cnt=0;
   q.push(start);
   while(!q.empty())
   {
           pre=q.front();
           q.pop();
           if(pre.x==end.x&&pre.y==end.y&&pre.z==end.z)
           {
               cout<<"Escaped in "<<pre.cnt<<" minute(s)."<<endl;
               return;
        } 
        int i;
        for(i=0;i<6;i++)
        {
            int nx=pre.x+dir[i][0];
            int ny=pre.y+dir[i][1];
            int nz=pre.z+dir[i][2];
            if(nx>=0&&nx<R&&ny>=0&&ny<C&&nz>=0&&nz<L&&mmap[nz][nx][ny]!='#'&&!vis[nz][nx][ny])//也可以写成mmap[nz][nx][ny]=='.'||mmap[nz][nx][ny]=='E' 但不要只写 =='.' 
            {
                nxt.x=nx;
                nxt.y=ny;
                nxt.z=nz;
                nxt.cnt=pre.cnt+1;
                q.push(nxt);
                vis[nz][nx][ny]=1;
            }
        }
   }

       cout<<"Trapped!"<<endl;
}
int main()
{
    while(scanf("%d%d%d",&L,&R,&C)&&L&&R&&C)
    {
        int i,j,k; 
        for(i=0;i<L;i++)
        {
            for(j=0;j<R;j++)
            {
                scanf("%s",&mmap[i][j]);
            }
            getchar();
            getchar();
        }
        for(i=0;i<L;i++)
        {
            for(j=0;j<R;j++)
            {
                for(k=0;k<C;k++)
                {
                    if(mmap[i][j][k]=='S')
                    {
                        start.x=j;
                        start.y=k;
                        start.z=i;
                    }
                    else if(mmap[i][j][k]=='E')
                    {
                        end.x=j;
                        end.y=k;
                        end.z=i;
                    }
                }
            }
        }
        bfs();
    }     
}

 


posted @ 2020-02-16 17:00  脂环  阅读(180)  评论(0编辑  收藏  举报