0分求助
查看原帖
0分求助
1351155
liuzhuoran141516楼主2024/9/22 09:43
#include <bits/stdc++.h>
using namespace std;
struct jgt {
	int v;
}t[505][505];
int n, m, a, b;
void f(jgt a[505][505], int n, int m, int x, int y) {
	for (int i = x + 1,j = x - 1; i <= n or j >= 1; i ++, j --) {
		for (int k = y + 1, l = y - 1; k <= m or l >= 1; k ++, l --) {
			if (i <= n and k <= m) {
				if (a[i][k].v == 1005) {
					int c = abs(i + k - (x + y)) / 2 + 1;
					a[i][k].v = c;
				} else {
					int c = abs(i + k - (x + y)) / 2 + 1;
					a[i][k].v = min(a[i][k].v, c);
				}
			}
			if (j >= 1 and l >= 1) {
				if (a[j][l].v == 1005) {
					int c = abs(j + l - (x + y)) / 2 + 1;
					a[j][l].v = c;
				} else {
					int c = abs(j + l - (x + y)) / 2 + 1;
					a[j][l].v = min(a[j][l].v, c);
				}
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> a >> b;
	for(int i = 1; i <= 504 ; i ++) {
		for(int j = 1; j <= 504 ; j ++) {
			t[i][j].v = 1005;
		}
	}
	for (int i = 1; i <= a; i ++) {
		int s1, s2;
		cin >> s1 >> s2;
		t[s1][s2].v = 0;
		f(t, n, m, s1, s2);
	}
	for (int i = 1; i <= b; i ++) {
		int s1, s2;
		cin >> s1 >> s2;
		cout << t[s1][s2].v << '\n';
	}
	return 0;
}
2024/9/22 09:43
加载中...