```cpp
#include<bits/stdc++.h>
using namespace std;
int n,m,step[505][505],a,b,x,y;
bool f[505][505];
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
struct XY
{
int x,y;
};
queue<XY>s;
void bfs()
{
XY cur;
while(!s.empty())
{
cur=s.front();
s.pop();
for(int i=0;i<4;i++)
{
int nx=cur.x+dx[i],ny=cur.y+dy[i];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(f[nx][ny])continue;
f[nx][ny]++;
s.push({nx,ny});
step[nx][ny]=step[cur.x][cur.y]+1;
}
}
return;
}
int main()
{
cin>>n>>m>>a>>b;
while(a--)
{
cin>>x>>y;
s.push({x,y});
f[x][y]=true;
}
bfs();
while(b--)
{
cin>>x>>y;
cout<<step[x][y]<<"\n";
}
return 0;
```
先看代码,能看懂就不用私信我,没看懂私信
~~求关~~
by zyhe2013 @ 2024-03-30 16:28:29
@[zyhe2013](/user/905073)
关了
by GG_and_go_to_died @ 2024-04-05 22:03:37