[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 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模版题,三维bfs
 1 #include<iostream>
 2 #include<string.h>
 3 #include<queue>
 4
 5 using namespace std;
 6
 7 int l,r,c,di,dj,dk,escape,ans,si,sj,sk;
 8 char mapp[35][35][35];
 9 int flag[35][35][35];
10 int dir[6][3]={{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};
11
12 struct node
13 {
14     int x,y,z;
15     int step;
16 };
17 node qi,zhong;
18 bool judge(int x,int y,int z)
19 {
20     if(x>=0 && x<l && y>=0 && y<r && z>=0 && z<c && !flag[x][y][z] && mapp[x][y][z]!=‘#‘)
21         return true;
22     return false;
23 }
24 void bfs()
25 {
26     queue<node>que;
27     que.push(qi);
28     node a,b;
29     while(!que.empty())
30     {
31         a = que.front();
32         que.pop();
33
34         if(a.x==zhong.x && a.y==zhong.y && a.z==zhong.z)
35         {
36             cout<<"Escaped in "<<a.step<<" minute(s)."<<endl;
37             return ;
38         }
39         for(int i = 0; i < 6; i++)
40         {
41             b=a;
42             b.x+=dir[i][0];
43             b.y+=dir[i][1];
44             b.z+=dir[i][2];
45             if(judge(b.x,b.y,b.z))
46             {
47                 flag[b.x][b.y][b.z]=1;
48                 b.step++;
49                 que.push(b);
50             }
51         }
52     }
53     cout<<"Trapped!"<<endl;
54 }
55 int main()
56 {
57     while(cin>>l>>r>>c,l||r||c)
58     {
59         memset(flag,0,sizeof(flag));
60         for(int i = 0; i < l; i++)
61         {
62             for(int j = 0; j < r; j++)
63             {
64                 cin>>mapp[i][j];
65                 for(int k = 0; k < c; k++)
66                 {
67                     if(mapp[i][j][k]==‘S‘)
68                         qi.x=i,qi.y=j,qi.z=k,qi.step=0;
69                     if(mapp[i][j][k]==‘E‘)
70                         zhong.x=i,zhong.y=j,zhong.z=k;
71                 }
72             }
73         }
74         bfs();
75
76     }
77
78     return 0;
79 }
				
时间: 2024-10-04 10:16:01

[kuangbin带你飞]专题一 简单搜索 bfs B - Dungeon Master的相关文章

kuangbin带你飞专题一 简单搜索 题解

目录 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题一 简单搜索 总结:用时2天半终于把这个专题刷完了 对于最基础的dfs bfs 路径打印 状态转移也有了一点自己些微的理解 其实2天半可以压缩到1天半的 主要是自己太懒了...慢慢加油刷bin神的专题呀 从大二下学期开始学算法 一开始就知道这个专题 一开始对于这个专题里的所有问题感觉都好难啊..就直接放弃了 看lrj的书 现在看到这个专题还挺唏嘘的吧 突然觉得思维和想法也不是很难 果然是那个时候心不静&还是储量不够吗

[kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple

1 //Memory Time 2 //2236K 32MS 3 4 #include<iostream> 5 using namespace std; 6 7 int mod[524286]; //保存每次mod n的余数 8 //由于198的余数序列是最长的 9 //经过反复二分验证,436905是能存储198余数序列的最少空间 10 //但POJ肯定又越界测试了...524286是AC的最低下限,不然铁定RE 11 12 int main(int i) 13 { 14 int n; 15

[kuangbin带你飞]专题一 简单搜索 棋盘问题

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

[kuangbin带你飞]专题一 简单搜索

一直拖.放了放久.受不了 A - 棋盘问题 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第

[kuangbin带你飞]专题一 简单搜索 A - 棋盘问题

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

Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two m

[kuangbin带你飞]专题一 简单搜索 - M - 非常可乐

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 bool cup[105][105][105]; 8 struct dot 9 { 10 int a; 11 int b; 12 int c; 13 int s; 14 }; 15 int x, y,

[kuangbin带你飞]专题一 简单搜索 - L - Oil Deposits

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 char g[105][105]; 8 int x, y, ans; 9 int dx[3]={1,0,-1}; 10 int dy[3]={1,0,-1}; 11 bool sscanf() 12

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

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 diagonal