• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU-2102 A计划 多维度地图BFS

题目

A计划

*Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45816 Accepted Submission(s): 11399
*

Problem Description

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。

Input

输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小NM(1 <= N,M <=10)。T如上所意。接下去的前NM表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。

Output

如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。

Sample Input

1
5 5 14
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..

Sample Output

YES

Source

HDU 2007-6 Programming Contest

思路 BFS

​ 没啥好写的就是需要注意一些问题吧,一开始审题有问题,还以为是给定两个地图,然后衔接起来进行BFS,没想到是两层进行BFS,还需要注意时空传送机不需要任何时间,而且下一层是墙的话,那么就不进行入队

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <utility>

using namespace std;


char a[15][15][3];

int n, m, t;

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

int d[15][15][3];

bool flag, st[15][15][3];

struct tuples {
	int x, y, z;

	// bool operator< (tuples b) const {
	// 	if (a[this->x][this->y][this->z] == '#') return true;
	// 	return false;
	// }
};


// void dfs(int x, int y, int z) {
// 	// cout << x << " " << y << " " << z << endl;
// 	if (flag) {
// 		return;
// 	}
// 	if (a[x][y][z] == '#' && z == 1) {
// 		if (a[x][y][z + 1] != '*') {
// 			st[x][y][z + 1] = true;
// 			dfs(x, y, z + 1);
// 		}
// 		return;
// 	}

// 	if(a[x][y][z] == 'P') {
// 		flag = true;
// 		return;
// 	}

// 	for (int i = 0; i <= 3; i ++) {
// 		int tx = x + dx[i], ty = y + dy[i];
// 		if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty][z] != '*' && !st[tx][ty][z]) {
// 			st[tx][ty][z] = true;
// 			dfs(tx, ty, z);
// 		}
// 	}
// }


void bfs() {
	queue<tuples> q1;
	tuples start = {1, 1, 1};
	memset(d, -1, sizeof d);
	memset(st, 0, sizeof st);
	d[1][1][1] = 0;
	q1.push(start);
	while (q1.size()) {
		tuples t1 = q1.front(); q1.pop();
		int x = t1.x, y = t1.y, z = t1.z;
		tuples next;

		if (a[x][y][z] == 'P') {
			if (d[x][y][z] <= t) {
				flag = true;
			}
			// cout << d[x][y][z] << endl;
			return;
		}

		for (int i = 0; i <= 3; i ++) {
			int tx = x + dx[i], ty = y + dy[i];
			if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty][z] != '*' && !st[tx][ty][z]) {
				st[tx][ty][z] = true;
				if(a[tx][ty][z] == '#') {
					int tz;
					if (z == 1) tz = 2;
					else tz = 1;
					if (a[tx][ty][tz] != '*' && a[tx][ty][tz] != '#' && d[tx][ty][tz] == -1) {
						next.x = tx, next.y = ty, next.z = tz;
						d[tx][ty][tz] = d[x][y][z] + 1;
						st[tx][ty][tz] = true;
						q1.push(next);
					}
					continue;
				}
				d[tx][ty][z] = d[x][y][z] + 1;
				next.x = tx, next.y = ty, next.z = z;
				q1.push(next);
			}
		}
	}
}

int main() {
	int _; 
	cin >> _;
	while (_ --) {
		memset(a, 0, sizeof a);
		flag = false;
		cin >> n >> m >> t;
		for (int z = 1; z <= 2; z ++) {
			for (int i = 1; i <= n; i ++) {
				for (int j = 1; j <= m; j ++) {
					cin >> a[i][j][z];
				}
			}
		}
		bfs();
		puts(flag?"YES":"NO");
	}
}
posted on 2023-01-13 14:58  Jack404  阅读(19)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3