uva 532 Dungeon Master(BFS)
2015-02-02 13:37
316 查看
uva 532 Dungeon Master
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 andthe maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input Specification
The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integersL, 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 Specification
Each maze generates one line of output. If it is possible to reach the exit, print a line of the formEscaped 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<stdio.h> #include<string.h> int move[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}}; //上下前后左右 struct pos { int x, y, z; int time; }; char map[40][40][40]; int vis[40][40][40], head, tail, min, flag; pos p[1000000]; void BFS() { int px, py, pz; while (head < tail) { if (map[p[head].x][p[head].y][p[head].z] == 'E') { if (p[head].time < min) min = p[head].time; flag = 1; } for (int i = 0; i < 6; i++) { px = p[head].x + move[i][0]; py = p[head].y + move[i][1]; pz = p[head].z + move[i][2]; if (map[px][py][pz] != '#' && map[px][py][pz] != '@' && !vis[px][py][pz]) { vis[px][py][pz] = 1; p[tail].x = px; p[tail].y = py; p[tail].z = pz; p[tail].time = p[head].time + 1; tail++; } } head++; } } int main() { int L, R, C; while (scanf("%d %d %d", &L, &R, &C) == 3, L, R, C) { memset(map, 0, sizeof(map)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= L; i++) { for (int j = 1; j <= R; j++) { getchar(); for (int k = 1; k <= C; k++) { scanf("%c", &map[i][j][k]); if (map[i][j][k] == 'S') { p[1].x = i; p[1].y = j; p[1].z = k; } } } getchar(); } for (int i = 0; i <= L + 1; i++) { for (int j = 0; j <= R + 1; j++) { map[i][j][0] = '@'; map[i][j][C + 1] = '@'; } } for (int i = 0; i <= L + 1; i++) { for (int j = 0; j <= C + 1; j++) { map[i][0][j] = '@'; map[i][R + 1][j] = '@'; } } for (int i = 0; i <= R + 1; i++) { for (int j = 0; j <= C + 1; j++) { map[0][i][j] = '@'; map[L + 1][i][j] = '@'; } } vis[p[1].x][p[1].y][p[1].z] = 1; head = 1; tail = 2; flag = 0; min = 9999; p[1].time = 0; BFS(); if (flag) printf("Escaped in %d minute(s).\n", min); else printf("Trapped!\n"); } return 0; }
相关文章推荐
- [uva-532]Dungeon Master (BFS)
- UVA 532 & POJ 2251 Dungeon Master (BFS)
- uva 532 Dungeon Master —— BFS(求最短路)
- UVA 532- Dungeon Master(三维bfs)
- UVA 532- Dungeon Master (BFS)
- UVa 532 Dungeon Master( BFS )
- uva 532 Dungeon Master(三维bfs)
- UVa 532 & POJ 2251 - Dungeon Master
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
- UVA 532 Dungeon Master
- UVa 532 - Dungeon Master
- UVa 532 - Dungeon Master
- UVA 532 - Dungeon Master
- UVA Dungeon Master (三维BFS)
- UVA532 Dungeon Master
- UVA 532-Dungeon Master
- uva532 - Dungeon Master
- uva 532(bfs)
- UVA 532 - Dungeon Master
- UVA - 532 Dungeon Master