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

本文共 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

你可能感兴趣的文章
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>
LeetCode 159. Longest Substring with At Most Two Distinct Characters
查看>>
基本算法概论
查看>>
jquery动态移除/增加onclick属性详解
查看>>
JavaScript---Promise
查看>>
暖暖的感动
查看>>
Java中的日期和时间
查看>>
Django基于admin的stark组件创建(一)
查看>>
抛弃IIS,利用FastCGI让Asp.net与Nginx在一起
查看>>
C. Tanya and Toys_模拟
查看>>
springboot jar包运行中获取资源文件
查看>>
基于FPGA实现的高速串行交换模块实现方法研究
查看>>
Java Scala获取所有注解的类信息
查看>>
delphi ,安装插件
查看>>
case when then的用法-leetcode交换工资
查看>>
11.28.cookie
查看>>
BeanShell简介
查看>>