球球了,小的卡不了,大的不想看,来个尽量小的Hack吧!
#include <iostream>
#include <queue>
using namespace std;
const int N = 1005, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int n, m, a, b, c[N][N], ans[N];
bool v[N][N];
struct AC {
int x, y, res;
};
queue<AC> q;
void bfs() {
for (; !q.empty(); q.pop()) {
int x = q.front().x, y = q.front().y, res = q.front().res;
if (c[x][y]) {
ans[c[x][y]] = min(ans[c[x][y]], res);
}
for (int i = 0; i < 4; i++) {
if (!v[x + dx[i]][y + dy[i]] && x + dx[i] <= n && y + dy[i] <= m && x + dx[i] >= 1 && y + dy[i] >= 1) {
v[x + dx[i]][y + dy[i]] = 1;
q.push({x + dx[i], y + dy[i], res + 1});
}
}
}
}
int main() {
cin >> n >> m >> a >> b;
for (int i = 1, x, y; i <= a; i++) {
cin >> x >> y;
v[x][y] = 1;
q.push({x, y, 0});
}
for (int i = 1, x, y; i <= b; i++) {
cin >> x >> y;
c[x][y] = i;
ans[i] = 1e9;
}
bfs();
for (int i = 1; i <= b; i++) {
cout << ans[i] << '\n';
}
return 0;
}