博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2251 Dungeon Master(3D迷宫 bfs)
阅读量:4878 次
发布时间:2019-06-11

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

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28416   Accepted: 11109

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!

思路

迷宫问题简单变形,一个3D迷宫,给你起点终点,找出最短路径。

#include
#include
#include
#include
using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 35;char maze[maxn][maxn][maxn];int dis[maxn][maxn][maxn];int L, R, C;struct Node{ int z, x, y; Node(int zz, int xx, int yy): z(zz), x(xx), y(yy) {}};int bfs(int sz, int sx, int sy, int gz, int gx, int gy){ queue
que; int dx[5] = { -1, 0, 1,0}, dy[5] = {0, -1, 0, 1}, dz[3] = {0, -1, 1}; memset(dis, INF, sizeof(dis)); dis[sz][sx][sy] = 0; que.push(Node(sz, sx, sy)); while (!que.empty()) { Node pos = que.front(); que.pop(); if (pos.z == gz && pos.x == gx && pos.y == gy) { break; } for (int i = 0; i < 3; i++) { if (i) { int nz = pos.z + dz[i], nx = pos.x, ny = pos.y; if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF) { que.push(Node(nz, nx, ny)); dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1; } } else { for (int j = 0; j < 5; j++) { int nz = pos.z + dz[i], nx = pos.x + dx[j], ny = pos.y + dy[j]; if (nz >= 0 && nz < L && nx >= 0 && nx < R && ny >= 0 && ny < C && maze[nz][nx][ny] != '#' && dis[nz][nx][ny] == INF) { que.push(Node(nz, nx, ny)); dis[nz][nx][ny] = dis[pos.z][pos.x][pos.y] + 1; } } } } } return dis[gz][gx][gy];}int main(){ //freopen("input.txt", "r", stdin); while (~scanf("%d%d%d", &L, &R, &C) && L && R && C) { int sz, sx, sy, gz, gx, gy; for (int i = 0; i < L; i++) { for (int j = 0; j < R; j++) { scanf("%s", maze[i][j]); for (int k = 0; k < C; k++) { if (maze[i][j][k] == 'S') { sz = i, sx = j, sy = k; } if (maze[i][j][k] == 'E') { gz = i, gx = j, gy = k; } } } getchar(); } bfs(sz, sx, sy, gz, gx, gy) == INF ? printf("Trapped!\n") : printf("Escaped in %d minute(s).\n", dis[gz][gx][gy]); } return 0;}

  

 

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/6123020.html

你可能感兴趣的文章
比特币入门
查看>>
Lucene教程
查看>>
http post 接口
查看>>
Python Cookbook(第2版)中文版
查看>>
Codeforces 980E The Number Games 贪心 倍增表
查看>>
省市区三级联动(二)JS部分简单版
查看>>
如何撰写总体设计与详细设计文档
查看>>
php开启openssl的方法,openssl安装
查看>>
集合类转化成数组
查看>>
什么是Java 虚拟机?为什么Java 被称作是"平台无关的编程语言"?
查看>>
better-scroll和swiper使用中的坑
查看>>
css之字体的引用
查看>>
oo第一单元博客
查看>>
实验报告三
查看>>
基础搜索
查看>>
vs2008 C# 怎么调试C++ dll[转]
查看>>
PHP的魔术方法
查看>>
警惕麦咖啡的"缓冲区溢出保护"引起的ASP.NET 中 System.OutOfMemoryException 的错误...
查看>>
optimizer_dynamic_sampling
查看>>
HTML(WEB)开发day05
查看>>