样例过了,但只有 #1 #4 #5 对了
#include <bits/stdc++.h>
using namespace std;
int n , m , e , r , x , y , v[600][600] = {} , l[1001][3] = {};
char a[1001][1001];
int dx[] = {1 , -1 , 0 , 0};
int dy[] = {0 , 0 , 1 , -1};
struct node {
int x , y , cnt;
node(int x1 , int y1 , int cnt1):x(x1) , y(y1) , cnt(cnt1) {}
};
int bfs(int , int);
int bfs(int q , int w) {
queue<node>p;
for (int i = 0 ; i <= n ; i ++)
for (int j = 0 ; j <= m ; j ++)
v[i][j] = 10000000;
for (int i = 0 ; i < e ; i ++) {
cin >> q >> w;
p.push(node(q , w , 0));
v[q][w] = 0;
a[q][w] = '+';
}
for (int i = 1 ; i <= r ; i ++) {
cin >> q >> w;
l[i][1] = q;
l[i][2] = w;
a[q][w] = '*';
}
while(!p.empty()) {
node cur = p.front();
p.pop();
for (int i = 0 ; i < 4 ; i ++) {
int _x = cur.x + dx[i];
int _y = cur.y + dy[i];
if (_x > 0 && _y > 0 && _x <= n && _y <= m && v[_x][_y] == 10000000) {
p.push(node(_x , _y , cur.cnt + 1));
v[_x][_y] = cur.cnt + 1;
}
}
}
return 0;
}
int main() {
cin >> n >> m >> e >> r;
bfs(1 , 1);
for (int i = 1 ; i <= r ; i ++)
cout << v[l[i][1]][l[i][2]] << endl ;
return 0;
}