博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dungeon Master ZOJ 1940【优先队列+广搜】
阅读量:6111 次
发布时间:2019-06-21

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

Problem 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!
 

//一次AC。感觉太爽了。!

#include
#include
#include
using namespace std;char map[40][40][40];int x1,y1,z1,x2,y2,z2;int dx[]={0,1,0,-1,0,0};int dy[]={1,0,-1,0,0,0};int dz[]={0,0,0,0,1,-1};int n,row,column;struct Dungeon//地牢 { int x,y,z; int time; friend bool operator < (Dungeon a,Dungeon b) { return a.time>b.time; }};bool judge(int xt,int yt,int zt){ if(xt>row||xt<1||yt>column||yt<1||zt>n||zt<1) //越界 return 0; if(map[zt][xt][yt]=='#') return 0; return 1;}int BFS(){ priority_queue
q; Dungeon pos,next; pos.x=x1; pos.y=y1; pos.z=z1; pos.time=0; map[z1][x1][y1]='#'; q.push(pos); while(!q.empty()) { pos=q.top(); q.pop(); for(int i=0;i<6;++i) { next.x=pos.x+dx[i]; next.y=pos.y+dy[i]; next.z=pos.z+dz[i]; next.time=pos.time+1; if(judge(next.x,next.y,next.z)) { if(next.x==x2&&next.y==y2&&next.z==z2) { return next.time; } map[next.z][next.x][next.y]='#'; q.push(next); } } } return -1;}int main(){ while(~scanf("%d%d%d",&n,&row,&column),n+row+column) { getchar(); for(int i=1;i<=n;++i) { for(int j=1;j<=row;++j) { for(int k=1;k<=column;++k) { scanf("%c",map[i][j]+k); if(map[i][j][k]=='S') { z1=i,x1=j,y1=k; } else if(map[i][j][k]=='E') { z2=i,x2=j,y2=k; } } getchar(); } getchar(); } int res; res=BFS(); if(res==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",res); } return 0;}

转载地址:http://bmkka.baihongyu.com/

你可能感兴趣的文章
熟练掌握doc命令下的文件操作
查看>>
Oracle中drop user和drop user cascade的区别
查看>>
【Linux】linux经常使用基本命令
查看>>
Java 内存区域和GC机制
查看>>
更新代码和工具,组织起来,提供所有博文(C++,2014.09)
查看>>
HTML模块化:使用HTML5 Boilerplate模板
查看>>
登记申请汇总
查看>>
Google最新截屏案例详解
查看>>
2015第31周一
查看>>
2015第31周日
查看>>
在使用EF开发时候,遇到 using 语句中使用的类型必须可隐式转换为“System.IDisposable“ 这个问题。...
查看>>
PHP使用DES进行加密和解密
查看>>
Oracle 如何提交手册Cluster Table事务
查看>>
BeagleBone Black第八课板:建立Eclipse编程环境
查看>>
在服务器上用Fiddler抓取HTTPS流量
查看>>
文件类似的推理 -- 超级本征值(super feature)
查看>>
【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误--备用...
查看>>
各大公司容器云的技术栈对比
查看>>
记一次eclipse无法启动的排查过程
查看>>
【转】jmeter 进行java request测试
查看>>