博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2251-Dungeon Master (三维空间求最短路径)
阅读量:6967 次
发布时间:2019-06-27

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

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! 开三维数组用BFS求。
1 #include
2 #include
3 using namespace std; 4 int dx[6]={-1,1,0,0,0,0}; 5 int dy[6]={
0,0,-1,1,0,0}; 6 int dz[6]={
0,0,0,0,1,-1}; 7 char str[100][100][100]; 8 int l,r,c,i,j,k,ans,bx,bz,by; 9 struct stu10 {11 int x,y,z,step;12 };13 bool f(stu st)14 {15 if(st.x<0 || st.z<0 || st.y<0 || st.x>=r || st.y>=c || st.z>=l || str[st.z][st.x][st.y] =='#')16 {17 return false;18 }19 return true;20 }21 int bfs(int bz,int bx,int by)22 {23 str[bz][bx][by]='#';24 int nx,ny,time,zz,xx,yy;25 queue
que;26 stu st,next;27 st.x=bx;28 st.y=by;29 st.z=bz;30 st.step=0;31 que.push(st);32 33 while(!que.empty())34 { 35 36 st=que.front();37 que.pop();38 39 xx=st.x;40 yy=st.y;41 zz=st.z;42 time=st.step;43 for(i = 0 ;i < 6 ; i++)44 {45 st.x=xx+dx[i];46 st.y=yy+dy[i];47 st.z=zz+dz[i];48 if(!f(st))49 {50 continue;51 }52 if(str[st.z][st.x][st.y] == 'E')53 {54 return time+1;55 }56 57 st.step=time+1;58 que.push(st);59 str[st.z][st.x][st.y]='#';60 }61 }62 return 0;63 }64 int main()65 {66 while(scanf("%d %d %d",&l,&r,&c)&&l&&r&&c)67 {68 for(i = 0 ; i < l ; i++)69 {70 for(j = 0 ; j < r ;j++)71 {72 scanf("%s",str[i][j]);73 for(k = 0 ; k < c ; k++)74 {75 if(str[i][j][k] == 'S')76 {77 bz=i; 78 bx=j;79 by=k;80 }81 }82 }83 }84 ans=bfs(bz,bx,by);85 if(ans)86 printf("Escaped in %d minute(s).\n",ans);87 else88 printf("Trapped!\n");89 90 }91 }

 

转载于:https://www.cnblogs.com/yexiaozi/p/5720197.html

你可能感兴趣的文章
导入自定义模块model
查看>>
App数据分析的五大维度!
查看>>
MyBatis框架使用(一)
查看>>
MySQL索引分析
查看>>
css中常用的标签
查看>>
C++中关键字的理解--Static
查看>>
html搜索,文中的关键字变色
查看>>
Python标准库_ sys,random,time
查看>>
GP通过外部表装载数据时遇到ERROR:extra data after last expected column解决方法
查看>>
C#开发中碰到的问题------Uncaught TypeError: Cannot read property 'style' of undefined
查看>>
Android 网络编程
查看>>
正则表达式
查看>>
Tomcat & SVN
查看>>
推荐系统学习03-SVDFeature
查看>>
mysql启动和关闭外键约束的方法
查看>>
安装 Docker <一>
查看>>
C#中的Dictionary字典类介绍
查看>>
PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用
查看>>
Microsoft Visual Studio 下载转帖
查看>>
证券交易买进卖出手续费公式
查看>>