呃呃呃,蒟蒻好不容易调到编译正常发现样例输出全是0,就很抽象
代码在这里,求各位大佬帮忙纠错(错误可能很低级qwq 大佬不要在意 我就是个蒟蒻)
写注释是个人习惯qwq 本来还想教教同学 发现自己不会(什
#include <iostream>
#include <algorithm>
#include <cstring>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 510;
int n, m, a, b;
int dist[N][N];
PII q[N * N];
int hh = 0, tt = -1;
//定义向量数组(四联通)
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
void bfs()
{
while(hh <= tt)
{
auto t = q[hh++];
//遍历四个方向,因为感染源可以往四周传染
for(int i = 0; i < 4; i++)
{
int a = t.x + dx[i], b = t.y + dy[i];
//查看有没有越界
if(a < 1 || a > n || b < 1 || b > m) continue;
if(dist[a][b] >= 0) continue;
dist[a][b] = dist[t.x][t.y];
q[++tt] = {a, b};
}
}
}
int main()
{
scanf("%d %d %d %d", &n, &m, &a, &b);
//初始化
memset(dist, -1, sizeof dist);
//输入感染源
while(a--)
{
int x, y;
scanf("%d %d", &x, &y);
q[tt++] = {x, y};
dist[x][y] = 0;
}
bfs();
//bfs扫完直接输入领主的位置,直接输出他在第几轮被感染
while(b--)
{
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", dist[x][y]);
}
return 0;
}