Dungeon Master

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

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!ps:和最基础的迷宫问题一样,只是2维变3维
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 int l,r,c,ans;
 8 char a[222][222][222];
 9 bool b[222][222][222];
10 int nx[6]={-1,1,0,0,0,0};
11 int ny[6]={0,0,-1,1,0,0};
12 int nz[6]={0,0,0,0,-1,1};
13 struct node{
14     int x;
15     int y;
16     int z;
17     int step;
18 };
19 int BFS(int z,int x,int y)
20 {
21     node s;
22     s.x=x;
23     s.y=y;
24     s.z=z;
25     s.step=0;
26     b[z][x][y]=true;
27     queue<node>q;
28     q.push(s);
29     while(!q.empty())
30     {
31         node now=q.front();
32         q.pop();
33         if(a[now.z][now.x][now.y]==‘E‘)
34         return now.step;
35         for(int i = 0;i < 6;i++)
36         {
37             node end;
38             end.x=now.x+nx[i];
39             end.y=now.y+ny[i];
40             end.z=now.z+nz[i];
41             end.step=now.step;
42             if(b[end.z][end.x][end.y]==false&&(a[end.z][end.x][end.y]==‘.‘||a[end.z][end.x][end.y]==‘E‘))
43             {
44                 b[end.z][end.x][end.y]=true;
45                 end.step+=1;
46                 q.push(end);
47             }
48
49         }
50     }
51     return -1;
52
53 }
54 int main()
55 {
56     while(scanf("%d %d %d",&l,&r,&c),l+r+c)
57     {
58         int i,j,k;
59         memset(b,false,sizeof(b));
60         memset(a,‘#‘,sizeof(a));
61         for(i = 0;i < l;i++)
62         {
63             for(j = 0;j < r;j++)
64             scanf("%s",a[i][j]);
65         }
66         for(i = 0;i < l;i++)
67             for(j = 0;j < r;j++)
68                 for(k = 0;k <c;k++)
69                 {
70                     if(a[i][j][k]==‘S‘)
71                     {
72                         ans=BFS(i,j,k);
73                         break;
74                     }
75                 }
76         if(ans == -1)
77         printf("Trapped!\n");
78         else
79         printf("Escaped in %d minute(s).\n",ans);
80
81
82     }
83     return 0;
84 }  //这个题应该可以优化!。
时间: 2024-10-15 08:23:00

Dungeon Master的相关文章

hdu 2251 Dungeon Master bfs

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17555   Accepted: 6835 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

UVA532 Dungeon Master

问题链接:UVA532 Dungeon Master. 题意简述:三维空间地牢(迷宫),每个点由'.'(可以经过).'#'(墙).'S'(起点)和'E'(终点)组成.移动方向有上.下.左.右.前和后6个方向.每移动一次耗费1分钟,求从'S'到'E'最快走出时间.不同L层,相同RC处是连通的. 问题分析:一个三维迷宫,典型的BFS问题.在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少. 程序中,使用了一个队列来存放中间节点,但是每次用完需要清空.需要注意的一点,为了编

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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

ZOJ 1940 Dungeon Master 三维BFS

Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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 r

UVA 532 Dungeon Master

题目如下: Dungeon Master  You are trapped in a 3D dungeon and need to find the quickest way out!The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minuteto move one unit north,south, east, west, up or down. You ca

poj2251:Dungeon Master

最初没有注意到结果是要求最小的步数,那么就成了最基本的迷宫找到一条出路的问题并记下找到出路时,所花的步数,那么很容易得到代码如下: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 6 #define MAX 1000000 7 int l,r,c,coun; 8 char m[30][30][30]; 9 bool flag[30][30]

POJ 2251 Dungeon Master(bfs)

Dungeon Master 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 dow

POJ:Dungeon Master(三维bfs模板题)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 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

[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master

B - 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 c