博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2251 Dungeon Master —— BFS
阅读量:4926 次
发布时间:2019-06-11

本文共 3522 字,大约阅读时间需要 11 分钟。

题目链接:

 

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37296   Accepted: 14266

 

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

 

题解:

三维的纯BFS。

 

 

代码如下:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const int INF = 2e9; 16 const LL LNF = 9e18; 17 const int MOD = 1e9+7; 18 const int MAXN = 30+10; 19 20 struct node 21 { 22 int x, y, z, step; 23 }; 24 25 int L, R, C; 26 char m[MAXN][MAXN][MAXN]; 27 int vis[MAXN][MAXN][MAXN]; 28 int dir[6][3] = { { 1,0,0},{ 0,1,0},{ 0,0,1},{-1,0,0},{ 0,-1,0},{ 0,0,-1} }; 29 30 queue
que; 31 int bfs(node s, node e) 32 { 33 ms(vis,0); 34 while(!que.empty()) que.pop(); 35 36 s.step = 0; 37 vis[s.x][s.y][s.z] = 1; 38 que.push(s); 39 40 while(!que.empty()) 41 { 42 node now = que.front(); 43 que.pop(); 44 45 if(now.x==e.x && now.y==e.y && now.z==e.z) 46 return now.step; 47 48 for(int i = 0; i<6; i++) 49 { 50 node tmp; 51 tmp.x = now.x + dir[i][0]; 52 tmp.y = now.y + dir[i][1]; 53 tmp.z = now.z + dir[i][2]; 54 if(tmp.x>=1 && tmp.x<=L && tmp.y>=1 && tmp.y<=R && tmp.z>=1 && tmp.z<=C 55 && m[tmp.x][tmp.y][tmp.z]!='#' && !vis[tmp.x][tmp.y][tmp.z] ) 56 { 57 vis[tmp.x][tmp.y][tmp.z] = 1; 58 tmp.step = now.step + 1; 59 que.push(tmp); 60 } 61 } 62 } 63 return -1; 64 } 65 66 int main() 67 { 68 while(scanf("%d%d%d",&L, &R, &C) && (L||R||C)) 69 { 70 for(int i = 1; i<=L; i++) 71 for(int j = 1; j<=R; j++) 72 scanf("%s", m[i][j]+1); 73 74 node s, e; 75 for(int i = 1; i<=L; i++) 76 for(int j = 1; j<=R; j++) 77 for(int k = 1; k<=C; k++) 78 { 79 if(m[i][j][k]=='S') s.x = i, s.y = j, s.z = k; 80 if(m[i][j][k]=='E') e.x = i, e.y = j, e.z = k; 81 } 82 83 int ans = bfs(s,e); 84 if(ans==-1) 85 printf("Trapped!\n"); 86 else 87 printf("Escaped in %d minute(s).\n", ans); 88 } 89 return 0; 90 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538599.html

你可能感兴趣的文章
面向对象目的选层电梯作业总结
查看>>
Tensorflow图像处理
查看>>
版本号的意义
查看>>
Java基础学习总结——Java对象的序列化和反序列化
查看>>
java运算符
查看>>
Poj3468 A Simple Problem with Integers (分块)
查看>>
级联保存
查看>>
Python自学知识点----Day02
查看>>
phpcms 大杂烩
查看>>
Matlab 函数ndims简介,flipdim简介
查看>>
关于MAVEN找不到JDK的那点事
查看>>
Eclipse 各种小图标的含义
查看>>
Set和Map数据结构
查看>>
内置对象Cookie和Session有何不同【常见面试题】
查看>>
【转载】Sqlserver数据库备份的几种方式
查看>>
静态链表的创建
查看>>
poll?transport=longpoll&connection...连接的作用
查看>>
fontconfig
查看>>
Toda 2
查看>>
Symfony 1.4 send mail embed image
查看>>