ZOJ 1940 Dungeon Master bfs
来源:互联网 发布:华大基因 云计算 编辑:程序博客网 时间:2024/05/17 03:34
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?
Is an escape possible? If yes, how long will it take?
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.
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.
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Escaped in 11 minute(s).Trapped!
深度搜索 以前是4个方向,现在为6个方向 在此基础上做一下变通就可以
PS 打代码的时间一定要细心,一次性打正确,如果不正确返回来找是很难发现的 。。就是粗心写错一个字符 我花了两个多小时找错。。哎(细心为大 ,不能一味的追求打字的速度,。,。)
#include<cstdio>#include<cstring>#include<queue>#include<iostream>using namespace std;const int M=33;const int inf=0x3f3f3f3f;char s[M][M][M];int d[M][M][M];int z,x,y;int sx,sy,sz;int ex,ey,ez;int nx,ny,nz;struct point{ int l,c,r;};//利用结构体来储存指标值int dz[6]={-1,1,0,0,0,0},dy[6]={0,0,-1,1,0,0},dx[6]={0,0,0,0,-1,1};int bfs(){ queue<point> que; point pp; pp.l=sz,pp.c=sx,pp.r=sy; que.push(pp); d[sz][sx][sy]=0; while(que.size()) { point p=que.front(); que.pop(); if(p.l==ez&&p.c==ex&&p.r==ey) break; for(int i=0;i<6;i++) { nz=p.l+dz[i]; nx=p.c+dx[i]; ny=p.r+dy[i]; if(nx>=0&&nx<x&&ny>=0&&ny<y&&nz>=0&&nz<z&&s[nz][nx][ny]!='#'&&d[nz][nx][ny]==inf) { point np; np.l=nz,np.c=nx,np.r=ny; que.push(np); d[nz][nx][ny]=d[p.l][p.c][p.r]+1; } } } return d[ez][ex][ey];}int main(){ int i,j,k; while(scanf("%d%d%d",&z,&x,&y)&&(x+y+z)) { memset(s,0,sizeof(s)); memset(d,inf,sizeof(d)); for(i=0;i<z;i++) { for(j=0;j<x;j++) { for( k=0;k<y;k++) { cin>>s[i][j][k]; if(s[i][j][k]=='S') { sx=j;sy=k;sz=i; } if(s[i][j][k]=='E') { ez=i;ex=j;ey=k; } } } } int res=bfs(); if(res==inf) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",res); } return 0;}
阅读全文
0 0
- ZOJ 1940 Dungeon Master 【BFS】
- zoj 1940 Dungeon Master(BFS)
- ZOJ 1940 Dungeon Master bfs
- zoj 1940 || poj 2251 Dungeon Master(bfs)
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- zoj 1940 Dungeon Master
- zoj - 1940 - Dungeon Master
- zoj 1940 Dungeon Master
- zoj 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- zoj 1940 Dungeon Master
- ZOJ 1940 Dungeon Master-BFS-(最短路径)
- zju 1940 Dungeon Master(三维BFS暴搜题)
- zoj 1940 || poj 2251 Dungeon Master
- ZOJ Problem Set - 1940 Dungeon Master
- 深度学习入门 ---softmax回归
- Azure Stack深入浅出5:在Azure Stack上使用Web App PaaS服务及其背后原理窥探
- 蓝创十周年庆,感恩有您陪伴
- WebSocket的原理与优缺点
- 算法复习:二叉搜索树的后序遍历序列
- ZOJ 1940 Dungeon Master bfs
- APP二维码微信扫描后无法下载的问题
- 排序算法
- MySQL命令操作
- linux下搭建smba文件服务器
- Android Mediaplayer播放时设置频谱
- HDU 1695 GCD (莫比乌斯反演入门学习小结)
- 对文件读取的几种方法
- CyclicBarrier