没想到这会是福建省历届夏令营的题目,估计也就是第一题的水平吧
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
int n, m, a, b;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int N = 510;
int dist[N][N];
bool st[N][N];
queue<PII> q;
void bfs()
{
while(q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int sx = t.x + dx[i], sy = t.y + dy[i];
if(sx < 1 || sx > n || sy < 1 || sy > m || st[sx][sy]) continue;
st[sx][sy] = true;
dist[sx][sy] = dist[t.x][t.y] + 1;
q.push({sx, sy});
}
}
}
int main()
{
cin >> n >> m >> a >> b;
memset(dist, -1, sizeof dist);
while(a -- )
{
int x, y;
cin >> x >> y;
dist[x][y] = 0;
st[x][y] = true;
q.push({x, y});
}
bfs();
while(b -- )
{
int x, y;
cin >> x >> y;
cout << dist[x][y] << endl;
}
return 0;
}