POJ 2251 Dungeon Master(dfs)

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!
就是一个人被逮到地牢里面了,地牢有好几层(L),每层R*C,标#的地方不能走,每次可以上、下、左、右、前、后走一个相邻的格子,花费1分钟。问你从S走,能不能逃出这个地牢(走到E)。这是我认真的写的第一个dfs,是不是有点晚呢?呵呵。总结一下dfs的几个要点吧。

1.struct node 用于标记点的坐标,和到达(如果能)该店的步数。2.vis dir 数组,分别表示  是否访问过  每次走的方向3.check函数 :检查3点 1->是否超出边界 2->是否走过 3->是否能走4.基于队列实现的bfs函数,这里详细解释首先我们建立一个node的队列q,我们先把起点加进队列。从当q的队首开始,用temp取得这个队首,q.pop().将通过temp能走到的node再加入到队列里面,同时判断是否到达终点。这样知道q这个队列空了,我们就走完了所有可能的路了。

代码如下:
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cmath>
 4 #include <queue>
 5 #include <cstring>
 6 using namespace std;
 7 struct node
 8 {
 9     int l,x,y;
10     int step;
11 };
12 int L,R,C;
13 int el,ex,ey,sl,sx,sy,ans;
14 char mp[40][40][40];
15 bool vis[40][40][40];
16 int dir[6][3]={1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1};
17 bool check(node now)
18 {
19     if (now.l>=L||now.l<0||
20         now.x>=R||now.x<0||
21         now.y>C||now.y<0)
22     return 0;
23     if (mp[now.l][now.x][now.y]==‘#‘||vis[now.l][now.x][now.y])
24     return 0;
25     return 1;
26 }
27 void bfs (int level,int xx,int yy)
28 {
29     queue<node> q;
30     node now;
31     now.l=level,now.x=xx,now.y=yy,now.step=0;
32     vis[level][xx][yy]=true;
33     q.push(now);
34     while (!q.empty())
35     {
36         node temp=q.front();
37         q.pop();
38         for (int i=0;i<6;++i)
39         {
40             now.l=temp.l+dir[i][0];
41             now.x=temp.x+dir[i][1];
42             now.y=temp.y+dir[i][2];
43             now.step=temp.step+1;
44             if (check(now))
45             {
46                 if (mp[now.l][now.x][now.y]==‘E‘)
47                 {
48                     ans=now.step;
49                     return;
50                 }
51                 vis[now.l][now.x][now.y]=true;
52                 q.push(now);
53             }
54         }
55     }
56 }
57 int main()
58 {
59     //freopen("de.txt","r",stdin);
60     while (~scanf("%d%d%d",&L,&R,&C))
61     {
62         if (L==0&&R==0&&C==0)
63         break;
64         el=ex=ey=0;
65         sl=sx=sy=0;
66         memset(vis,false,sizeof vis);
67         for (int i=0;i<L;++i)
68         {
69             for (int j=0;j<R;++j)
70             {
71                 for (int k=0;k<C;++k)
72                 {
73                     cin>>mp[i][j][k];
74                     if (mp[i][j][k]==‘S‘)
75                     {
76                         sl=i,sx=j,sy=k;
77                     }
78                     if (mp[i][j][k]==‘E‘)
79                     {
80                         el=i,ex=j,ey=k;
81                     }
82                 }
83             }
84         }
85         ans=-1;
86         bfs(sl,sx,sy);
87         if (ans!=-1)
88         printf("Escaped in %d minute(s).\n",ans);
89         else
90         printf("Trapped!\n");
91     }
92     return 0;
93 }
 
时间: 2024-10-13 22:04:38

POJ 2251 Dungeon Master(dfs)的相关文章

poj 2251 Dungeon Master(bfs)

题目链接  http://poj.org/problem?id=2251 题意:一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped! 简单的三维bfs随便做一下就可以了. #include <iostream> #include <cstring&g

POJ 2251 Dungeon Master (bfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; char mat[50][50][50]; int vis[50][50][50]; int op[6][3]={0,-1,0, 0,1,0, 1,0,0, -1,0,0 ,0,0,1, 0,0,-1 }; int ok; in

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

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

POJ - 2251 - Dungeon Master (简单BFS)

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

棋盘问题(DFS)&amp; Dungeon Master (DFS)

1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目. n <= 8 , k <= n 当为-1 -1时表示输入结束. 随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域,

POJ - 2251 Dungeon Master(三维BFS)

题目链接:http://poj.org/problem?id=2251 题意:三维BFS. 题解:大水题,只不过多加了两个方向 1 //poj2251 2 #include <queue> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int sx,sy,sz,ex,ey,ez,L,R,C; 8 const int INF=

poj 2251 Dungeon Master(简单三维广搜)

题意: 之前做过的所有题都是   在一个平面   上搜索 . 本题很新,在一个三维空间里 ,首先   l  x  y   分别代表 l 层   每一层有 x 行   y 列 问从 S 开始   走到 E   最小步是多少    显然用广搜,只是多了一个向上向下的搜索. 注意: 所谓广搜  ,是一层一层的搜,  每一层其步数是一样的   ,可以通过建立一个数组存步数 ,也可以 将步数同时存入队列中 另外搜过的要做标记, 在做标记时  尽量要在里面做标记(就是每搜过一个点就  立马 将之标记)不然一

POJ 2251 Dungeon Master(三维空间bfs)

题意:三维空间求最短路,可前后左右上下移动. 分析:开三维数组即可. #include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAXN = 30 + 10; char pic[MAXN][MAXN][MAXN]; bool vis[MAXN][MAXN][MAXN]; int sx, sy, sz; int dr[] = {0, 0, 1, -1, 0, 0};