首页 > 其他 > 详细

uva 532 Dungeon Master(BFS)

时间:2015-02-02 14:09:41      阅读:313      评论:0      收藏:0      [点我收藏+]

                             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 and the 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 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 Specification 

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<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)

原文:http://blog.csdn.net/llx523113241/article/details/43407291

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!