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

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

Output

Escaped in 11 minute(s).
Trapped!
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
const int N =50 ;
char Map[N][N][N];
int dz[] = {-1,1,0,0,0,0};
int dx[] = {0,0,-1,1,0,0};
int dy[] = {0,0,0,0,-1,1};
int u,n,m; 
int a,b,c;
int d,e,f;
bool vis[N][N][N];
struct que
{
    int z,x,y,t;
};
queue<que> q;
bool check(int z,int x,int y)
{
    if(z<0||z>=u||x<0||x>=n||y<0||y>=m) return false;//越界 
    if(vis[z][x][y]) return false;//已经走过 
    if(Map[z][x][y]=='#') return false;//墙壁 
    
    return true;
}
void bfs()
{
    que temp;
    temp.z = a;
    temp.x = b;
    temp.y = c;
    temp.t = 0;
    q.push(temp);
    vis[a][b][c] = true;
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        
        for(int i=0;i<6;i++)
        {
            int zz = temp.z+dz[i];
            int xx = temp.x+dx[i];
            int yy = temp.y+dy[i];
            if(!check(zz,xx,yy)) continue;//非法 
            
            if(zz==d&&xx==e&&yy==f)//到达终点 
            {
                printf("Escaped in %d minute(s).\n",temp.t+1);
                return;
            }
            que tt;
            tt.z = zz;
            tt.x = xx;
            tt.y = yy;
            tt.t = temp.t+1;
            q.push(tt);
            vis[zz][xx][yy] = true;
        }
    }
    printf("Trapped!\n");
}
int main()
{
    while(scanf("%d %d %d",&u,&n,&m)!=EOF)
    {
        if(u==0&&n==0&&m==0) break;
        memset(vis,false,sizeof(vis));
        q = queue<que>();//初始化 
        for(int i=0;i<u;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%s",Map[i][j]);
                for(int k=0;k<m;k++)
                {
                    if(Map[i][j][k]=='S')//记录起点 
                    {
                        a = i;
                        b = j;
                        c = k;
                    }
                    if(Map[i][j][k]=='E')//记录终点 
                    {
                        d = i;
                        e = j;
                        f = k;
                    }
                }
            }
            getchar();
        }
        bfs();
    } 
    return 0;
} 
目录
相关文章
|
3月前
|
安全
8-17|2023-08-16 12:33:55,972 [salt.master :1643][ERROR ][20321] Received minion error from [m
8-17|2023-08-16 12:33:55,972 [salt.master :1643][ERROR ][20321] Received minion error from [m
|
4月前
|
开发工具 git
Push to origin/master was rejected
这篇文章提供了解决在IDEA中遇到"Push to origin/master was rejected"错误的步骤,包括执行特定的git pull命令并使用`--allow-unrelated-histories`选项来合并不相关的仓库历史。
|
7月前
|
Java
HDU-1551-Cable master
HDU-1551-Cable master
26 0
|
7月前
|
流计算
POJ 3620--Avoid The Lakes【DFS】
POJ 3620--Avoid The Lakes【DFS】
35 0
Dungeon Master(BFS广度优先搜索)
Dungeon Master(BFS广度优先搜索)
43 0
CF1365D Solve The Maze (BFS)
CF1365D Solve The Maze (BFS)
86 0
CF1365D Solve The Maze (BFS)
CF711D-Directed Roads(组合数学 dfs找环)
CF711D-Directed Roads(组合数学 dfs找环)
86 0
CF711D-Directed Roads(组合数学 dfs找环)
codeforces1244——D.Paint the Tree(dfs)
codeforces1244——D.Paint the Tree(dfs)
87 0