样例是对的,感觉思路也没啥错
#include<bits/stdc++.h>
using namespace std;
int dx[]={1,-1,0,0};
int dy[]={0,0,-1,1};
bool f[1001][1001];
int l[10001][3];
int t[1001][1001];
int n,m,a,b,x,y,tx,ty;
queue<int> xx;
queue<int> yy;
queue<int> s;
int main(){
cin>>n>>m>>a>>b;
for(int i=1;i<=a;i++){
cin>>x>>y;
xx.push(x);
yy.push(y);
s.push(0);
f[x][y]=1;
t[x][y]=0;
}
for(int i=1;i<=b;i++){
cin>>l[i][1]>>l[i][2];
}
while(!xx.empty()){
int nx,ny;
for(int i=0;i<4;i++){
nx=xx.front()+dx[i];
ny=yy.front()+dy[i];
if(nx>0&&nx<=n&&ny>0&&ny<=m&&f[nx][ny]==0){
xx.push(nx);
yy.push(ny);
s.push(s.front()+1);
f[nx][ny]=1;
}
}
t[xx.front()][yy.front()]=s.front();
xx.pop();
yy.pop();
s.pop();
}
for(int i=1;i<=b;i++){
cout<<t[l[i][1]][l[i][2]]<<endl;
}
return 0;
}