社区讨论

求条闭关

P3395路障参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@milgexzw
此快照首次捕获于
2025/11/30 16:24
3 个月前
此快照最后确认于
2025/12/02 22:21
3 个月前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
int n, t;
int bl[2005][2005];
int vis[1005][1005];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
struct node {
	int x, y, tim;
};
bool bfs() {
	queue<node> q;
	q.push({1, 1, 0});
	vis[1][1] = 0;

	while (!q.empty()) {
		node cur = q.front();
		q.pop();

		if (cur.x == n && cur.y == n) {
			return true;
		}

		for (int i = 0; i < 4; i++) {
			int nx = cur.x + dx[i];
			int ny = cur.y + dy[i];
			int nt = cur.tim + 1;

			if (nx >= 1 && nx <= n && ny >= 1 && ny <= n) {
				if (vis[nx][ny] == -1 && (bl[nx][ny] == -1 || nt < bl[nx][ny])) {
					vis[nx][ny] = nt;
					q.push({nx, ny, nt});
				}
			}
		}
	}
	return false;
}
int main() {
	cin >> t;
	while (t--) {
		cin >> n;
		memset(bl, -1, sizeof(bl));
		memset(vis, -1, sizeof(vis));
		for (int i = 1; i <= 2 * n - 2; i++) {
			int x, y;
			cin >> x >> y;
			if (bl[x][y] == -1) {
				bl[x][y] = i;
			}
		}
		if (bfs()) {
			cout << "Yes" << endl;
		} else {
			cout << "NO" << endl;
		}
	}
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...