uva 532 Dungeon Master(BFS)

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;
}
时间: 2024-11-13 23:37:31

uva 532 Dungeon Master(BFS)的相关文章

poj 2251 Dungeon Master(bfs)

题目链接  http://poj.org/problem?id=2251 题意:一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped! 简单的三维bfs随便做一下就可以了. #include <iostream> #include <cstring&g

POJ 2251 Dungeon Master (bfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; char mat[50][50][50]; int vis[50][50][50]; int op[6][3]={0,-1,0, 0,1,0, 1,0,0, -1,0,0 ,0,0,1, 0,0,-1 }; int ok; in

UVA 532 Dungeon Master

题目如下: Dungeon Master  You are trapped in a 3D dungeon and need to find the quickest way out!The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minuteto move one unit north,south, east, west, up or down. You ca

棋盘问题(DFS)&amp; Dungeon Master (DFS)

1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目. n <= 8 , k <= n 当为-1 -1时表示输入结束. 随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域,

POJ 2251 Dungeon Master(dfs)

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 m

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

uva 10651 Pebble Solitaire (BFS)

uva 10651 Pebble Solitaire Pebble solitaire is an interesting game. This is a game where you are given a board with an arrangement of small cavities, initially all but one occupied by a pebble each. The aim of the game is to remove as many pebbles as

UVA 10047 - The Monocycle(BFS)

题目链接:点击打开链接 题意:从起点到终点,每秒可以选择前进.向左.向右转, 每前进一格轮子转到下一个颜色, 一共5中颜色, 开始的时候绿色接触地面,朝北, 要求最后也绿色接触地面,求能否到达目标点以及最短时间. 思路:和普通BFS相比,多了两个附加条件,所以要将状态表示全面,也要对应加两维. 水题. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream&g

uva 11624 Fire!(BFS)

Fire! Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem B: Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a f