0分求调
查看原帖
0分求调
1076328
YHL_RP楼主2024/12/25 21:04
#include<bits/stdc++.h>
using namespace std;

const int N = 500;
int mes[N][N];
bool vis[N][N];
queue<pair<int, int>> texy;
queue<pair<int, int>> q;
int GoX[4] = {-1, 0, 1, 0};
int GoY[4] = {0, 1, 0, -1};

int main() {
	int n, m, a, b;
	cin >> n >> m >> a >> b;
	for (int i = 1; i <= a; i++) {
		int x, y;
		cin >> x >> y;
		q.push({x, y});
	}
	for (int i = 1; i <= b; i++) {
		int x, y;
		cin >> x >> y;
		texy.push({x, y});
	}

	int level = 0;
	while (q.size()) {
		int Psize = q.size();
		for (int k = 1; k <= Psize; k++) {
			int x = q.front().first;
			int y = q.front().second;
			q.pop();
			mes[x][y] = level;
			for (int i = 0; i < 4; i++) {
				int tx = x + GoX[i];
				int ty = y + GoY[i];
				if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty]) {
					vis[tx][ty] = true;
					q.push({tx, ty});
				}
			}
		}
		level++;
	}

	while (texy.size()) {
		int x = texy.front().first;
		int y = texy.front().second;
		texy.pop();
		cout << mes[x][y] << endl;
	}
	return 0;
}
2024/12/25 21:04
加载中...