算法实验三 Problem F木乃伊迷宫

该博客讨论了一个关于在6x6的迷宫中逃脱木乃伊追赶的问题。玩家和木乃伊都有特定的移动规则,玩家的目标是避开木乃伊到达出口。通过输入迷宫布局、木乃伊和玩家的初始位置,使用广度优先搜索(BFS)算法判断是否能安全逃脱。示例输入和输出展示了迷宫的结构和移动策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem F

木乃伊迷宫

时限:1000ms 内存限制:10000K 总时限:3000ms

描述:

木乃伊地下宫殿是一个6行6列的迷宫。作为敢到木乃伊地下宫殿里去探险的你,有没有跟木乃伊抓迷藏的心理准备呵!游戏在木乃伊所在的迷宫里展开,任务就是尽快赶到出口。你一次只能走一步,而木乃伊可以走两步,但木乃伊是很笨的,他总是先尽量跟你达到同一列,如果已经是同一列了,他才会像你走来,有墙的地方人和木乃伊都不能过,你可以利用障碍物牵制住木乃伊。

输入:

先输入墙的数量n,然后在后续的n行里每行有3个数表示一堵墙,3个数分别为格子的行、列和墙的位置(0表示这个格子的下方是墙,1表示这个格子的右方是墙),再下来的3行每行2个数,分别表示木乃伊、人还有出口的位置。

输出:

    如果能安全逃生则输出Yes,否则输出No,答案占一行。

输入样例:

5
0 0 0
1 1 1
1 4 1
3 4 1
4 3 0
3 3
3 1
5 5

输出样例:

No

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

struct rec {
	int mx, my;
	int px, py;
	bool useful;
};
queue <rec> q;
int visited[7][7][7][7];
int maze[8][8][2];
int n;//墙的数量
int mstart_x, mstart_y, pstart_x, pstart_y, target_x, target_y;

int dx[4] = {0, 1, 0, -1};

int dy[4] = {-1, 0, 1, 0};

void init() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x, y, w;
		cin >> x >> y >> w;
		maze[x][y][w] = 1;

	}
	cin >> mstart_x >> mstart_y;
	cin >> pstart_x >> pstart_y;
	cin >> target_x >> target_y;
}

bool iswall(int x, int y, int i) {
	switch (i) {
		case 0://向左走
			return maze[x][y - 1][1];

		case 1://向下走
			return maze[x][y][0];

		case 2://向右走
			return maze[x][y][1];

		case 3://向上走
			return maze[x - 1][y][0];
	}
	return false;
}

rec move(rec now, int i) {
	rec next;
	next.px = now.px + dx[i];
	next.py = now.py + dy[i];
	if (next.py < 0 || next.py > 5 || next.px < 0 || next.px > 5 || iswall(now.px, now.py, i)) {
		next.useful = false;
		return next;
	}
	int step = 2;
//	cout << endl;
//	cout << "木乃伊的位置:";
//	cout << now.mx << now.my << endl;
//	cout << "人的位置:";
//	cout << next.px << next.py << endl;
	next.mx = now.mx, next.my = now.my;
	while (step--) {
		if (next.py > next.my && !iswall(next.mx, next.my, 2)) {
			next.my++;
		} else if (next.py < next.my && !iswall(next.mx, next.my, 0)) {
			next.my--;
		} else if (next.py == next.my) {
			if (next.px > next.mx && !iswall(next.mx, next.my, 1)) {
				next.mx++;
			} else if (next.px < next.mx && !iswall(next.mx, next.my, 3)) {
				next.mx--;
			} else if (next.px == next.mx) {
//				cout << next.px << ' ' << next.py << "catch" << endl;
				next.useful = false;
				return next;
			}
		}
	}
	if (visited[next.px][next.py][next.mx][next.my]) {
		next.useful = false;
	} else {
		next.useful = true;
		visited[next.px][next.py][next.mx][next.my] = 1;
	}
	return next;
}

bool bfs() {
	rec temp;
	temp.mx = mstart_x, temp.my = mstart_y;
	temp.px = pstart_x, temp.py = pstart_y;
	visited[temp.px][temp.py][temp.mx][temp.my] = 1;
	q.push(temp);
	while (q.size()) {
		rec now = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			rec next = move(now, i);
			if (next.useful == true) {
				if (next.px == target_x && next.py == target_y) {
					return true;
				}
				q.push(next);
			}
		}
	}

	return false;
}

int main() {
	init();
	if (bfs()) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值