poj 2251 Dungeon Master

救命啊!!继续不懂。。继续看题解。。。

题目大意转自:優YoU http://user.qzone.qq.com/289065406/blog/1303446571

题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径

移动方向可以是上,下,左,右,前,后,六个方向

每移动一次就耗费一分钟,要求输出最快的走出时间。 不同L层的地图,相同RC坐标处是连通的

解题思路:

我越看这题就越觉得是  XX地下城 = =

水题一道,求最短路问题,直接BFS得了

开三维数组,每次搜索方向由二维的4个方向增加到6个,但是方法还是那个方法

没难度

注意若果三维数组恰好开到极限的30*30*30是会RE的,别替人家电脑省空间,想AC就开大点。

值得一提的是。。。这题竟然被那篇经典的  POJ分类  文章归纳到DFS。。。网上发现几个同学还在郁闷地DFS。。。。

这里就提示一下大家,凡是看到求最短路,用DFS一定很难做出来,一定要BFS

题目:

                                                                                    
                                                                                                  Dungeon Master
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14217   Accepted: 5526

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!

代码如下:
 1 #include<stdio.h>
 2 int  s[30][30][30];
 3 int L,R,C;
 4 int x0,y0,z0,x1,y1,z1;
 5 int  c[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
 6 void init()
 7 {
 8     int i,j,k;
 9     for(i=0;i<L;getchar(),i++)
10         for(j=0;j<R;j++)
11             for(getchar(),k=0;k<C;k++)
12             {
13                char ch=getchar();
14                if(ch=='#')
15                     s[i][j][k]=-1;
16                else if(ch=='.')
17                    s[i][j][k]=0;
18                else if(ch=='S')   { x0=i; y0=j; z0=k; s[i][j][k]=0;}
19                else     { x1=i; y1=j; z1=k; s[i][j][k]=0;}
20             }
21 }
22 int bfs()
23 {
24     int b[30000][3];
25     int i,j,t1,t2,t3,t0,x,y,z;
26     b[0][0]=x0;
27     b[0][1]=y0;
28     b[0][2]=z0;
29     t1=0;
30     t0=t2=t3=1;
31     for(;t1<t2;t1=t2,t2=t3,t0++)
32         for(i=t1;i<t2;i++)
33         {
34             for(j=0;j<6;j++)
35             {
36                 x=b[i][0]+c[j][0];
37                 y=b[i][1]+c[j][1];
38                 z=b[i][2]+c[j][2];
39                 if(x>=0&&y>=0&&z>=0&&x<L&&y<R&&z<C&&s[x][y][z]==0)
40                 {
41                     b[t3][0]=x;
42                     b[t3][1]=y;
43                     b[t3][2]=z;
44                     s[x][y][z]=t0;
45                     t3++;
46                 }
47             }
48             if(s[x1][y1][z1]>0) return t0;
49         }
50         return 0;
51 }
52 int main()
53 {
54     int k;
55     scanf("%d%d%d",&L,&R,&C);
56     while (L>0)
57     {
58         init();
59         k=bfs();
60         if (k>0) printf("Escaped in %d minute(s).
",k);
61         else printf("Trapped!
");
62         scanf("%d%d%d",&L,&R,&C);
63     }
64     return 0;
65 }
View Code
原文地址:https://www.cnblogs.com/mafangfang/p/3283540.html