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).
",ans);
89         else
90         printf("Trapped!
");
91     }
92     return 0;
93 }
 
原文地址:https://www.cnblogs.com/agenthtb/p/6020235.html