poj 2251 Dungeon Master
2016-08-16 20:54
381 查看
Dungeon Master
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
Source
/*
题目大意:
给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径
移动方向可以是上,下,左,右,前,后,六个方向
每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的
*/
最短路就用广搜
代码菜鸟,如有错误,请多包涵!!!
如果有帮助记得鼓励我一下,谢谢!!!
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26930 | Accepted: 10552 |
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!
Source
/*
题目大意:
给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径
移动方向可以是上,下,左,右,前,后,六个方向
每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的
*/
最短路就用广搜
#include <string.h> #include <stdio.h> #include <algorithm> #include <algorithm> #include <queue> using namespace std; struct node { int x; int y; int z; int step; }; char Map[40][40][40]; int x1, y1, z1; int x2, y2, z2; int x,y,z; queue<node> q; int vis[40][40][40]; int dx[6] = {1, -1, 0, 0, 0, 0}; int dy[6] = {0, 0, 1, -1, 0, 0}; int dz[6] = {0, 0, 0, 0, 1, -1}; int bfs(int xx, int yy, int zz) { int i; while ( !q.empty() ) { q.pop(); } node temp; temp.x = xx; temp.y = yy; temp.z = zz; temp.step = 0; q.push(temp); while ( !q.empty() ) { node flag; node flag1; flag = q.front(); q.pop(); for ( i = 0;i < 6; i++ ) { if ( (flag.x+dx[i] >= 0&&flag.x+dx[i] < x) && (flag.y+dy[i] >= 0&&flag.y+dy[i] < y) && (flag.z+dz[i] >= 0&& flag.z+dz[i] < z) && (vis[flag.x+dx[i]][flag.y+dy[i]][flag.z+dz[i]] == 0) && (Map[flag.x+dx[i]][flag.y+dy[i]][flag.z+dz[i]] != '#') ) { flag1.x = flag.x+dx[i]; flag1.y = flag.y+dy[i]; flag1.z = flag.z+dz[i]; vis[flag1.x][flag1.y][flag1.z] = 1; flag1.step = flag.step+1; if (flag1.x == x2&&flag1.y == y2&&flag1.z == z2) { return flag1.step; } q.push(flag1); } } } return -1; } int main() { int i, j, k; while ( ~scanf ( "%d %d %d", &x, &y, &z ) && (x != 0&&y != 0&&z != 0) ) { memset(vis, 0, sizeof(vis)); for ( i = 0;i < x; i++ ) { for ( j = 0;j < y; j++ ) { scanf ( "%s", Map[i][j] ); for ( k = 0;k < z ; k++ ) { if ( Map[i][j][k] == 'S' ) { x1 = i; y1 = j; z1 = k; } if ( Map[i][j][k] == 'E' ) { x2 = i; y2 = j; z2 = k; } } } } vis[x1][y1][z1] = 1; int sum = bfs(x1, y1, z1); if ( sum == -1 ) printf ( "Trapped!\n" ); else printf ( "Escaped in %d minute(s).\n", sum ); } }
代码菜鸟,如有错误,请多包涵!!!
如果有帮助记得鼓励我一下,谢谢!!!
相关文章推荐
- POJ 2251 Dungeon Master
- poj 2251 Dungeon Master(BFS)
- POJ:2251 Dungeon Master(广搜)
- poj 2251 Dungeon Master 题解(bfs广搜)
- poj 2251 Dungeon Master
- Dungeon Master (POJ 2251)
- poj 2251:Dungeon Master
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master (BFS)
- B_Dungeon Master(POJ_2251)
- POJ 2251 Dungeon Master
- zoj 1940 || poj 2251 Dungeon Master
- BFS POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ2251——Dungeon Master
- POJ 2251-Dungeon Master(BFS)
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
- zoj 1940 || poj 2251 Dungeon Master(bfs)
- POJ2251 Dungeon Master