博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2251 Dungeon Master (三维BFS)
阅读量:7067 次
发布时间:2019-06-28

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

题目链接:

 

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 51402   Accepted: 19261

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! 题意: 在一个三维空间中,# 表示石块,不能走,. 表示路,可以走,求从 S 到 E 花费的最短时间,每走一步花费时间加 1 。 注意: 1.不要写 if(bfs()) printf("%d",bfs()),这种形式,要写一个中间变量 int ans = bfs() ,然后,if(ans)  printf("%d",ans); 2.这是一个三维空间,有六个方向 3.不要只判断当前位置是不是被标记过,还要判断当前位置是不是石头(“#”);
1 #include
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 const int maxn = 35; 9 int go[6][3] = {
0,0,1,0,0,-1,-1,0,0,1,0,0,0,1,0,0,-1,0};10 bool mark[maxn][maxn][maxn];11 char maze[maxn][maxn][maxn];12 int sx,sy,sz,ex,ey,ez;13 int L,R,C;14 struct node15 {16 int x,y,z;17 int time;18 };19 20 bool Isok(int x,int y,int z)21 {22 //别忘了判断是不是'#'23 return x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z < C && !mark[x][y][z] && maze[x][y][z] != '#';24 }25 int bfs()26 {27 queue
q;28 struct node cu,ne;29 cu.x=sx;cu.y=sy;cu.z=sz;30 cu.time = 0;31 mark[sx][sy][sz]=1;32 q.push(cu);33 while(!q.empty())34 {35 cu = q.front();36 q.pop();37 if(cu.x==ex&&cu.y==ey&&cu.z==ez)38 return cu.time;39 for(int i=0;i<6;i++)40 {41 ne.x = cu.x + go[i][0];42 ne.y = cu.y + go[i][1];43 ne.z = cu.z + go[i][2];44 if(Isok(ne.x,ne.y,ne.z))45 {46 mark[ne.x][ne.y][ne.z] = 1;47 ne.time = cu.time + 1;48 q.push(ne);49 }50 }51 }52 return 0;53 }54 int main()55 {56 while(~scanf("%d%d%d",&L,&R,&C)&&(L||R||C))57 {58 memset(mark,0,sizeof(mark));59 memset(maze,0,sizeof(maze));60 for(int i=0;i
Ac代码

 

转载于:https://www.cnblogs.com/cypblogs/p/10023557.html

你可能感兴趣的文章
android中自定义的dialog中的EditText无法弹出输入法解决方案
查看>>
Android 70道面试题汇总不再愁面试
查看>>
字符串和数字的全排列问题、前i位被i整除问题
查看>>
互联网我来了 -- 2. js中&quot;异步/堵塞&quot;等概念的简析
查看>>
Linux下用来获取各种系统信息的C++类
查看>>
深入浅出OpenStack云计算平台管理(nova-compute/network)
查看>>
Redis学习手册(Sorted-Sets数据类型)
查看>>
DAO模式
查看>>
Linux工具入门:make工具与Makefile文件
查看>>
Navicat Premium 连接 Oracle 数据库
查看>>
表达式拼接Expression<Func<IEntityMapper, bool>> predicate
查看>>
[改善Java代码]在switch的default代码块中增加AssertionError错误
查看>>
李洪强漫谈iOS开发[C语言-004]-开发概述程序设计语言程序编译过程
查看>>
[css]通过transform缩放邮件客户端h5页面
查看>>
微软要解决癌症问题?
查看>>
一个男人想经商,不读 100本商人自传,怎么会了解商人的思维状态
查看>>
Atitit 开发2d游戏的技术选型attilax总结
查看>>
Linux下检测IP地址冲突及解决方法
查看>>
Struts,Spring,Hibernate三大框架的面试
查看>>
SQLAlchemy ORM之建表与查询
查看>>