实在看不出来哪会 RE。
大佬求调!QwQ
#include <bits/stdc++.h>
#define ll long long
#define pii pair <int, int>
using namespace std;
const int N = 1e3 + 10;
struct node {
int x, y;
int times;
} ;
queue <node> q;
bool vis[N][N];
int ans[N][N];
int n, m, a, b;
int dx[5] = {0, -1, 0, 0, 1};
int dy[5] = {0, 0, -1, 1, 0};
inline int bfs() {
while (!q.empty()) {
node t = q.front(); q.pop();
int x = t.x, y = t.y, tim = t.times;
for (int i = 1; i <= 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (!(nx >= 1 && nx <= n && ny >= 1 && ny <= m)) continue;
if (vis[nx][ny]) continue;
ans[nx][ny] = min(tim + 1, ans[nx][ny]);
q.push({nx, ny, tim + 1});
vis[nx][ny] = true;
}
}
}
int main() {
cin >> n >> m >> a >> b;
memset(ans, 0x3f, sizeof ans);
for (int i = 1; i <= a; ++i) {
int x, y; cin >> x >> y;
q.push({x, y, 0});
vis[x][y] = true;
ans[x][y] = 0;
}
bfs();
for (int i = 1; i <= b; ++i) {
int x, y; cin >> x >> y;
cout << ans[x][y] << "\n";
}
return 0;
}