样例过了,但全 WA,求助。
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,ans[505][505];
const int ax[5] = {1,-1,0,0};
const int ay[5] = {0,0,1,-1};
struct peo
{
int x,y;
};
queue <peo> bfs;
int main()
{
cin >>n>>m>>a>>b;
int ix,iy;
for(int i = 0; i < a; i++)
{
cin >>ix>>iy;
ans[ix][iy] = 0;
bfs.push({ix,iy});
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
ans[i][j] = 1e9;
}
}
while(!bfs.empty())
{
peo now = bfs.front();
//cout <<now.x<<' '<<now.y<<"\n";
bfs.pop();
for(int i = 0; i < 4; i++)
{
int nx = now.x + ax[i],ny = now.y + ay[i];
if(nx > n || nx < 1 || ny > m || ny < 1)continue;
if(ans[nx][ny] == 1e9)
{
bfs.push({nx,ny});
ans[nx][ny] = ans[now.x][now.y] + 1;
}
}
}
int ox,oy;
for(int i = 0; i < b; i++)
{
cin >>ox>>oy;
cout <<ans[ox][oy]-1e9<<"\n";
}
}