UVA 532 - Dungeon Master

题目大意:从起点能否到达终点,如果能就输出起步数

解题思路:用 BFS 即可判断是否能到达终点切输出其最少步数

#include <cstdio>
#include <queue>
using namespace std;

int level, row, column;
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
char dungeon[35][35][35];
struct Node {
	int x;
	int y;
	int z;
	int step;
};

struct Node start;

int BFS() {
	queue<Node> Q;
	Q.push(start);
	while (!Q.empty()) {
		Node root;
		root = Q.front();
		Q.pop();
		for (int i = 0; i < 6; i++) {
			Node child;	
			child.z = root.z + dir[i][0];
			child.x = root.x + dir[i][1];
			child.y = root.y + dir[i][2];
			if (child.z < 0 || child.z >= level || child.x < 0 || child.x >= row || child.y < 0 || child.y >= column || dungeon[child.z][child.x][child.y] == '#')
				continue; //越界、碰墙就停止

			child.step = root.step + 1;

			if (dungeon[child.z][child.x][child.y] == 'E') {   //到达终点就输出起点到终点所需的 step
				printf("Escaped in %d minute(s).\n", child.step);
				return 0;
			}

			Q.push(child);
			dungeon[child.z][child.x][child.y] = '#'; //假如没到终点就标记已走过

		}

	}
	return 1;

}

int main() {
	while (scanf("%d%d%d", &level, &row, &column), level) {
		for (int i = 0; i < level; i++)
			for (int j = 0; j < row; j++) {
				scanf("%s", dungeon[i][j]);
				for (int k = 0; k < column; k++)
					if (dungeon[i][j][k] == 'S') {
						start.z = i;
						start.x = j;
						start.y = k;
						start.step = 0;
					}
			}

		if (BFS())
			printf("Trapped!\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰阔落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值