标签添加
查看原帖
标签添加
1277129
WangMouHongKe楼主2025/7/30 17:42

建议添加标签 数学
思路:一个点的传播时间是距离病源点的 曼哈顿距离 ,每次输入新的病源点时对整个矩阵更新。
结果我写了个* BFS *MLE 了 献上 MLE 代码:

#include <bits/stdc++.h>
using namespace std;
int nx, ny, i, n, m, a, b, x, y, ans[505][505], dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
bool vis[505][505];
struct value {
	int x, y, t;
} top;
queue<value> q;
int main() {
	ios::sync_with_stdio(0);
	cin >> n >> m >> a >> b;
	memset(ans, 0x3f, sizeof ans);
	while (a--) {
		cin >> x >> y;
		memset(vis, 0, sizeof vis);
		while (!q.empty()) q.pop();
		q.push({x, y, 0});
		while (!q.empty()) {
			top = q.front();
			q.pop();
			ans[top.x][top.y] = min(ans[top.x][top.y], top.t);
			vis[top.x][top.y] = 1;
			for (i = 0; i < 4; ++i) {
				nx = top.x + dx[i],
				ny = top.y + dy[i];
				if (vis[nx][ny] || nx < 1 || ny < 1 || nx > n || ny > m)
					continue;
				q.push({nx, ny, top.t + 1});
			}
		}
	}

	while (b--) {
		cin >> x >> y;
		cout << ans[x][y] << '\n';
	}
}

应该是可以优化的,但数学做法只用了19行!

2025/7/30 17:42
加载中...