暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

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!

问题分析:依旧是简单bfs,但是要用三维数组储存地图,但是有6个分支,用数组来储存方向可以让程序更简洁。

 1 #include "iostream"
 2 #include "queue"
 3 using namespace std;
 4 char maze[32][32][32];
 5 int v[6][3]={0,0,-1,0,0,1,0,-1,0,0,1,0,-1,0,0,1,0,0};
 6 int L,R,C;
 7 struct escaper
 8 {
 9     int i;
10     int j;
11     int k;
12     int time;
13 };
14 escaper fir;
15 void mbegin()
16 {
17    for (int i=0;i<=L+1;i++)
18     for (int j=0;j<=R+1;j++)
19      for (int k=0;k<=C+1;k++)
20         if (i*j*k == 0 || i == L+1 || j==R+1 || k==C+1)
21                   maze[i][j][k] = ‘#‘;
22        else
23        {
24             cin>>maze[i][j][k];
25          if (maze[i][j][k] == ‘S‘)
26               {
27                   fir.i = i;
28                   fir.j = j;
29                   fir.k = k;
30               }
31         }
32 }
33 void bfs()
34 {
35    queue <escaper> p;
36    escaper sec;
37    fir.time=0;
38    p.push(fir);
39      while (!p.empty())
40    {
41       sec = p.front();
42       p.pop();
43      for (int i=0;i<6;i++)
44       {
45          fir.i = sec.i+v[i][0];
46          fir.j = sec.j+v[i][1];
47          fir.k = sec.k+v[i][2];
48         if (maze[fir.i][fir.j][fir.k] != ‘#‘)
49           {
50               fir.time = sec.time+1;
51               if (maze[fir.i][fir.j][fir.k] == ‘E‘)
52                    {
53                        cout<<"Escaped in "<<fir.time<<" minute(s)."<<endl;
54                        return;
55                    }
56               maze[fir.i][fir.j][fir.k] = ‘#‘;
57               p.push(fir);
58           }
59       }
60    }
61     cout<<"Trapped!"<<endl;
62 }
63 int main()
64 {
65     while (cin>>L>>R>>C && L && R && C)
66     {
67          mbegin();
68           bfs();
69     }
70    return 0;
71 }

				
时间: 2024-12-24 09:29:11

暑假集训(1)第三弹 -----Dungeon Master(Poj2251)的相关文章

暑假集训(2)第三弹 ----- 食物链(poj1182)

C - 食物链 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关

暑假集训(4)第三弹 -----递推(Hdu1799)

问题描述:还记得正在努力脱团的小A吗?在他绘制贤者法阵时,他曾经最亲密的战友,暗中设下鬼打墙将小A围困,并准备破坏 小A正在绘制的法阵.小A非常着急.想阻止他的行动.而要阻止他,必须先破解鬼打墙. 哎,你不得不再次帮助他,告诉他在鬼打墙内应该走多少步.才能打破所有for循环. 问题分析:可以利用杨辉三角:1 1    1 1     2    1 1     3     3    1 1     4     6    4   1 ........ 1    a[i-1][j]+a[i-1][j]

暑假集训(2)第五弹 ----- Who&#39;s in the Middle(poj2388)

G - Who's in the Middle Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median

暑假集训(2)第七弹 -----今年暑假不AC(hdu2037)

J - 今年暑假不AC Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了. 作为球迷,一定想看尽量

暑假集训(4)第六弹——— 组合(poj1067)

题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走上这条路不过数日,好 歹你也帮助过许多生物脱离困境啊.怎么就好人......等等,你翻了翻包裹,拿出了一个颇为古旧的黄铜瓶,瓶口用锡 纸封着,这瓶子是上次那只青蛙送的,它说危难之际,打开瓶口便可解围.“娑殚之瓶!“,谁知刚拿出瓶子,就听到小A 惊讶的呼声,“它也许能帮我们脱离困境,不过恐怕会有些危险

暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

暑假集训(1)第八弹 -----Catch the cow(Poj3984)

Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. Input 一个5 × 5的二维数组,表示一个迷宫.数据保证有唯一解. Output 左上角到右下角的最短路径,格式如样例所示. Sa

暑假集训(4)第五弹——— 数论(hdu1222)

题意概括:那天以后,你好说歹说,都快炼成三寸不烂之舍之际,小A总算不在摆着死人脸,鼓着死鱼眼.有了点恢复的征兆.可孟子这家伙说的话还是有点道理,那什么天将降....额,总之,由于贤者法阵未完成,而小A又迟迟不现身,FFF团团长连下七道圣火令追杀你们,最先赶到地,机械化部队,它们除了智能不高外,可以说是无懈可击.这正是你要利用的一点,利用他们的行动轨迹,躲藏起来. 问题分析:首先用辗转相除法求得gcd(n,m),若n>m 则gcd(n,m)为一可逃反之,非一可逃. 1 #include "c

暑假集训(1)第六弹 -----简单计算器(Hdoj1237)

Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔.没有非法表达式.当一行中只有0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即该表达式的值,精确到小数点后2位. Sample Input 1 + 2 4 + 2 * 5 - 7 / 11 0 Sample Output 3.00 13.36 问题分析:难