POJ 2251 Dungeon Master :NYOJ 353 3D dungeon (三维空间上的搜索 bfs )
来源:互联网 发布:大货车压扁小轿车知乎 编辑:程序博客网 时间:2024/04/29 18:08
Dungeon Master
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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?
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.
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
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!
Sample Input
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
三维空间上的搜索,和二维搜索一样,只要把每个方向别弄混淆了,相信都没什么问题,这里我就不多说了,直接附上代码
附上代码:
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<queue>using namespace std;char str[35][35][35];int visit[35][35][35];int dx[6] = {0,0,-1,1,0,0}, // 三维六个方向搜索 分别表示 x y z 的变化 dy[6] = {-1,1,0,0,0,0}, dz[6] = {0,0,0,0,-1,1};int x2,y2,z2,X,Y,Z;struct num{int x,y,z,step;};queue<num>Q;int bfs(int x1,int y1,int z1){num e = {x1,y1,z1,0};Q.push(e);visit[z1][y1][x1] = 1;while(!Q.empty()){e = Q.front();if(e.x == x2 && e.y == y2 && e.z == z2)break;Q.pop(); // 切记这个清空队列要在判断过是否为终点以后清除,因为有个能有可能队列的最后一个是符合条件的,如果先清除,那么在下面判断时时候会是队列为空,那么就会出现错误for(int i = 0;i < 6;i++) // 对六个方向进行搜素{int sx = e.x + dx[i];int sy = e.y + dy[i];int sz = e.z + dz[i];if(sx >= 0 && sx < X && sy >= 0 && sy < Y && sz >= 0 && sz < Z && !visit[sz][sy][sx] && (str[sz][sy][sx] == '.' || str[sz][sy][sx] == 'E')) // 是 ‘.’ 和 ‘E’且 没有搜索过的,进行进队列搜索{num e1 = {sx,sy,sz,e.step+1};Q.push(e1);visit[sz][sy][sx] = 1; // 走过以后标记为 1} }}if(Q.empty())return -1;while(!Q.empty()) // 搜索结束完以后进行队列清空操作{Q.pop();}return e.step;}int main(){int x1,y1,z1;while(scanf("%d%d%d",&Z,&Y,&X) && (X != 0 || Y != 0 || Z != 0)){memset(visit,0,sizeof(visit));for(int i = 0;i < Z;i++){for(int j = 0;j < Y;j++){scanf("%s",str[i][j]);for(int k = 0;k < X;k++){if(str[i][j][k] == 'S'){z1 = i;y1 = j;x1 = k;}if(str[i][j][k] == 'E'){z2 = i;y2 = j;x2 = k;}}}}int sum = bfs(x1,y1,z1);if(sum == -1)printf("Trapped!\n");elseprintf("Escaped in %d minute(s).\n",sum);}return 0;}
0 0
- POJ 2251 Dungeon Master :NYOJ 353 3D dungeon (三维空间上的搜索 bfs )
- POJ2251:Dungeon Master(BFS)3D搜索
- POJ 2251 Dungeon Master 三维空间BFS(基础题)
- <三维BFS搜索> POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master (搜索 -- BFS)
- NYOJ 353 Dungeon Master【BFS】
- poj 2251 — Dungeon Master (3D + BFS)
- NYOJ 353 3D dungeon + zoj 1940 Dungeon Master
- POJ 2251 Dungeon Master(3维BFS)
- POJ 2251 Dungeon Master BFS
- POJ 2251 Dungeon Master (BFS)
- POJ 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master【BFS】
- POJ 2251 Dungeon Master(BFS)
- poj 2251 Dungeon Master(bfs)
- POJ 2251--Dungeon Master【BFS】
- poj 2251 Dungeon Master 【BFS】
- POJ 2251:Dungeon Master【bfs】
- Linux make方法安装文件是./configure问题解决方法
- 彻底了解mapreduce核心Shuffle
- PAT (Basic Level) Practise (中文)1029. 旧键盘(20)
- 解决在onCreate()过程中获取View的width和Height为0的4种方法
- OA软件在企业管理中的作用
- POJ 2251 Dungeon Master :NYOJ 353 3D dungeon (三维空间上的搜索 bfs )
- bzoj 4539: [Hnoi2016]树 主席树&lca
- BZOJ4318: OSU!
- Java 爬虫入门(网易云音乐和知乎实例)
- sqlite异常掉电相关问题(1)
- 单链表程序
- golang RSA加密解密程序
- 二分查找的一点改进优化
- Linux获取pci设备的厂商号、设备号、class号