poj 2251 Dungeon Master

来源:互联网 发布:dva防御矩阵用不了 编辑:程序博客网 时间:2024/06/16 15:28

题目链接:点击打开链接

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

ps:三维地图搜索求最短时间,从S到E

找不到输出Trapped!

<span style="font-size:24px;">//三维地图bfs搜索最短路径#include <iostream>#include<queue>#include<cstring>#include<cstdio>#include<cstdlib>using namespace std;struct node{    int x,y,z;    int step;}now,next;int L,R,C;char mp[50][50][50];int vis[50][50][50];int sx,sy,sz;int ex,ey,ez;int dx[]={-1,1,0,0,0,0};int dy[]={0,0,-1,1,0,0};int dz[]={0,0,0,0,1,-1};int bfs(int x,int y,int z){    queue<node>q;    while(!q.empty())q.pop();    now.x=x;    now.y=y;    now.z=z;    now.step=0;    q.push(now);    while(!q.empty())    {        now=q.front();        q.pop();        if(now.x==ex&&now.y==ey&&now.z==ez)        {            return now.step;        }        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];            if(nx>=0&&ny>=0&&nz>=0&&nz<L&&nx<R&&ny<C&&mp[nz][nx][ny]!='#'&&vis[nz][nx][ny]==0)            {                vis[nz][nx][ny]=1;                next.x=nx;                next.y=ny;                next.z=nz;                next.step=now.step+1;                q.push(next);            }        }    }    return -1;}int main(){    while(cin>>L>>R>>C)    {        if(L==0&&R==0&&C==0)break;        for(int i=0;i<L;i++)        {            for(int j=0;j<R;j++)            {                cin>>mp[i][j];                for(int k=0;k<C;k++)                {                    if(mp[i][j][k]=='S')                    {                        sx=j;                        sy=k;                        sz=i;                    }                    if(mp[i][j][k]=='E')                    {                        ex=j;                        ey=k;                        ez=i;                    }                }            }        }        memset(vis,0,sizeof(vis));        vis[sz][sx][sy]=1;       int k = bfs(sx,sy,sz);       if(k==-1)printf("Trapped!\n");       else printf("Escaped in %d minute(s).\n",k);    }    return 0;}</span>


0 0
原创粉丝点击