51,713
社区成员




啊啊
#include<bits/stdc++.h>
using namespace std;
const int N = 300001;
int h,t=1,g[501][501],st[501][501];
int fx[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int ans[N][2];
struct node{
int x,y;
}q[N];
int main()
{
int n,m,a,b;
cin>>n>>m>>a>>b;
for(int i=1;i<=a;i++)
{
cin>>q[t].x>>q[t].y;
st[q[t].x][q[t].y]=1;
t++;
}
while(h<t)
{
h++;
for(int i=0;i<4;i++)
{
int tx=q[h].x+fx[i][0],ty=q[h].y+fx[i][1];
if(tx>0&&ty>0&&tx<=n&&ty<=m&&st[tx][ty]!=1)
{
t++;
q[t].x=tx;q[t].y=ty;
st[tx][ty]=1;
g[tx][ty]=g[q[h].x][q[h].y]+1;
}
}
}
for(int i=1;i<=b;i++) cin>>ans[i][0]>>ans[i][1];
for(int i=1;i<=b;i++) cout<<g[ans[i][0]][ans[i][1]]<<endl;
return 0;
}