POJ2251(BFS求最短路)
来源:互联网 发布:java 输出两位小数 编辑:程序博客网 时间:2024/05/17 04:03
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!
题意:给你一个三维迷宫,让你算出从‘S’到‘E’的最短路径,如果没有通路,则输出”Trapped!”,否则输出”Escaped in x minute(s).”,x为最短路经过的路径数。
题解:从终点开始BFS,搜索出每一个能到达的点到终点的最短路径,当全部搜索完时,如果起点没有被搜索到,说明不通,否则起点到终点的最短路就是所要求得的结果。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <queue>#include <cmath>#include <vector>using namespace std;struct POINT { int l, r, c; POINT (int l = 0, int r = 0, int c = 0) : l(l), r(r), c(c) {}};const int maxn = 35;int d[maxn][maxn][maxn], vis[maxn][maxn][maxn], l, r, c;char maze[maxn][maxn][maxn];POINT s, e;void BFS(){ POINT cur; queue<POINT> q; q.push(e); vis[e.l][e.r][e.c] = 1; d[e.l][e.r][e.c] = 0; while (!q.empty()) { cur = q.front(); q.pop(); //printf ("%d %d %d ", cur.l, cur.r, cur.c); //printf ("路径%d\n\n", d[cur.l][cur.r][cur.c]); if (cur.c == s.c && cur.l == s.l && cur.r == s.r) { printf ("Escaped in %d minute(s).\n", d[s.l][s.r][s.c]); return; } if (cur.l + 1 < l && (maze[cur.l + 1][cur.r][cur.c] == '.' || maze[cur.l + 1][cur.r][cur.c] == 'S') && !vis[cur.l + 1][cur.r][cur.c]) { vis[cur.l + 1][cur.r][cur.c] = 1; d[cur.l + 1][cur.r][cur.c] = d[cur.l][cur.r][cur.c] + 1; POINT next(cur.l + 1, cur.r, cur.c); q.push(next); } if (cur.l - 1 >= 0 && (maze[cur.l - 1][cur.r][cur.c] == '.' || maze[cur.l - 1][cur.r][cur.c] == 'S') && !vis[cur.l - 1][cur.r][cur.c]) { vis[cur.l - 1][cur.r][cur.c] = 1; d[cur.l - 1][cur.r][cur.c] = d[cur.l][cur.r][cur.c] + 1; POINT next(cur.l - 1, cur.r, cur.c); q.push(next); } if (cur.r + 1 < r && (maze[cur.l][cur.r + 1][cur.c] == '.' || maze[cur.l][cur.r + 1][cur.c] == 'S') && !vis[cur.l][cur.r + 1][cur.c]) { vis[cur.l][cur.r + 1][cur.c] = 1; d[cur.l][cur.r + 1][cur.c] = d[cur.l][cur.r][cur.c] + 1; POINT next(cur.l, cur.r + 1, cur.c); q.push(next); } if (cur.r - 1 >= 0 && (maze[cur.l][cur.r - 1][cur.c] == '.' || maze[cur.l][cur.r - 1][cur.c] == 'S') && !vis[cur.l][cur.r - 1][cur.c]) { vis[cur.l][cur.r - 1][cur.c] = 1; d[cur.l][cur.r - 1][cur.c] = d[cur.l][cur.r][cur.c] + 1; POINT next(cur.l, cur.r - 1, cur.c); q.push(next); } if (cur.c + 1 < c && (maze[cur.l][cur.r][cur.c + 1] == '.' || maze[cur.l][cur.r][cur.c + 1] == 'S') && !vis[cur.l][cur.r][cur.c + 1]) { vis[cur.l][cur.r][cur.c + 1] = 1; d[cur.l][cur.r][cur.c + 1] = d[cur.l][cur.r][cur.c] + 1; POINT next(cur.l, cur.r, cur.c + 1); q.push(next); } if (cur.c - 1 >= 0 && (maze[cur.l][cur.r][cur.c - 1] == '.' || maze[cur.l][cur.r][cur.c - 1] == 'S')&& !vis[cur.l][cur.r][cur.c - 1]) { vis[cur.l][cur.r][cur.c - 1] = 1; d[cur.l][cur.r][cur.c - 1] = d[cur.l][cur.r][cur.c] + 1; POINT next(cur.l, cur.r, cur.c - 1); q.push(next); } } //printf ("%d\n\n", d[s.l][s.r][s.c]); printf ("Trapped!\n");}int main(){ #ifndef ONLINE_JUDGE freopen ("in.txt", "r", stdin); #endif // ONLINE_JUDGE while (scanf ("%d%d%d", &l, &r, &c) != EOF) { if (l == 0 && r == 0 && c == 0) break; for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { for (int k = 0; k < c; k++) { cin >> maze[i][j][k]; if (maze[i][j][k] == 'S') { s.l = i; s.r = j; s.c = k; } if (maze[i][j][k] == 'E') { e.l = i; e.r = j; e.c = k; } } } } //printf ("S : %d %d %d\n\n", s.l, s.r, s.c); //printf ("E : %d %d %d\n\n", e.l, e.r, e.c); memset (vis, 0, sizeof(vis)); memset (d, -1, sizeof(d)); BFS (); } return 0;}
- POJ2251(BFS求最短路)
- poj2251 三维bfs求最短距离
- POJ2251---Dungeon Master---BFS最短路之三维迷宫
- hdu 2354(bfs求最短路)
- POJ 3170(bfs求最短路)
- 魔戒-BFS求最短路
- UVALive7015(bfs求最短路)
- UVA816(用BFS求最短路)(wf)
- hdu 3760(2次bfs求最短路)
- uva439 - Knight Moves(BFS求最短路)
- [容易] BFS求最短路(迷宫类问题)
- 独轮车,白书P308Uva10047(BFS求最短路)
- POJ2243 Knight moves (BFS求最短路)
- poj 2215 bfs求最短路
- hdu 1242 用bfs求最短路
- ACM-icpc---bfs求最短路
- BFS(广搜)算法与模板 bfs求最短路
- BFS-最短路(迷宫)
- 【C++11】新特性——引入nullprt
- CodeForces 626A B - Robot Sequence
- 华为OJ 初级:质数因子
- 调试器gdb
- 安卓--ListView和Adapter的关系和如何使用实例
- POJ2251(BFS求最短路)
- 海量数据处理 算法总结
- CodeForces 665C C - Simple Strings
- Android基础——数据持久化存储
- 欢迎使用CSDN-markdown编辑器
- 中央缓存结合本地缓存-本地缓存数据刷新方案
- 100万个数中找出最大的前100个数
- 布隆滤波器(Bloom Filter)
- 路由 get '/*path' => 'welcome#index'