POJ - 2251 Dungeon Master(15.10.10 搜索专题)bfs
2015-10-11 22:01
676 查看
Dungeon Master
Submit Status
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
Sample Output
思路:简单题,三维空间的一个迷宫,求逃出的最少时间。三维空间上的bfs
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
思路:简单题,三维空间的一个迷宫,求逃出的最少时间。三维空间上的bfs
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; char map[50][50][50]; bool vis[50][50][50]; const int dirx[6] = { 1, -1, 0, 0, 0, 0 }; const int diry[6] = { 0, 0, 1, -1, 0, 0 }; const int dirz[6] = { 0, 0, 0, 0, 1, -1 }; int L, R, C; int sx, sy, sz; struct point { int x, y, z; int step; point() { step = 0; } point(int xx, int yy, int zz, int stepp) : x(xx), y(yy), z(zz), step(stepp){} }; bool islegal(int x, int y, int z) { if (x >= 0 && x<L && y>=0 && y<R && z>=0 && z < C && map[x][y][z] != '#' && !vis[x][y][z]) return true; return false; } int bfs() { queue<point>que; memset(vis, false , sizeof(vis)); point init(sx, sy, sz, 0); que.push(init); vis[sx][sy][sz] = true; while (!que.empty()) { point loc = que.front(); que.pop(); if (map[loc.x][loc.y][loc.z]=='E') return loc.step; for (int i = 0; i < 6; i++) { int xx = loc.x + dirx[i]; int yy = loc.y + diry[i]; int zz = loc.z + dirz[i]; int locstep = loc.step; if (islegal(xx, yy, zz)) { que.push(point(xx, yy, zz, locstep + 1)); vis[xx][yy][zz] = true; } } } return -1; } int main() { while (scanf("%d%d%d", &L, &R, &C) != EOF) { if (L == 0) break; for (int i = 0; i < L; i++) for (int j = 0; j < R; j++) cin >> map[i][j]; for (int i = 0; i < L; i++) for (int j = 0; j < R; j++) for (int k = 0; k < C; k++) { if (map[i][j][k] == 'S') { sx = i; sy = j; sz = k; break; } } int ans = bfs(); if (ans == -1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n", ans); } }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- JavaScript中数组的排序、乱序和搜索实现代码
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法
- 基于ASP.NET的lucene.net全文搜索实现步骤
- 做个自己站内搜索引擎
- PHP查找与搜索数组元素方法总结