uva 532 Dungeon Master(BFS)

  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 LR 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只是多了一个下标而已。

#includestdio.h
#includestring.h
#define N 50
struct coor{
	int x;
	int y;
	int z;
};

char map[N][N][N];
int vis[N][N][N];
int n, m, h, cnt;
coor que[N * N * N];
const int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};

int BFS(coor *head, coor *end){
	if (head == end)
		return 0;
	coor *move = end;
	while (head != end){
		for (int i = 0; i  6; i++){
			int a, b, c;
			a = head - x + dir[i][0];
			b = head - y + dir[i][1];
			c = head - z + dir[i][2];
			if (a  0 || a = n)	continue;
			if (b  0 || b = m)	continue;
			if (c  0 || c = h)	continue;
			if (vis[c][a][b])	continue;
			if (map[c][a][b] == '#')	continue;

			if (map[c][a][b] == 'E')	return 1;

			move - x = a;
			move - y = b;
			move - z = c;
			vis[c][a][b] = 1;
			move++;
		}
		head++;}
	cnt++;
	if (BFS(end, move))
		return 1;
	else
		return 0;
}

int judge(){
	for (int a = 0; a  h; a++)
		for (int b = 0; b  n; b++)
			for (int c = 0; c  m; c++)
				if (map[a][b][c] == 'S'){
					que[0].x = b, que[0].y = c, que[0].z = a;
					vis[a][b][c] = 1;
					if (BFS(que, que + 1))
						return 1;
					else
						return 0;
				}
}

int main(){
	while (scanf("%d%d%d", h, n, m), h  n  m){
		// Init.
		memset(map, 0, sizeof(map));
		memset(vis, 0, sizeof(vis));
		memset(que, 0, sizeof(que));
		cnt = 0;

		// Read.
		for (int k = 0; k  h; k++){
			getchar();
			for (int i = 0; i  n; i++)
				gets(map[k][i]);
		}

		// Handle.
		if(judge())
			printf("Escaped in %d minute(s).\n", cnt + 1);
		else
			printf("Trapped!\n");
	}
	return 0;}

最新回复(0)
/jishuNvJH4Y02T5Z4ec2yMQoY5PnucQuxJx_2FeTbR5pLvvSqs_3D4795034
8 简首页