중심 좌표가 xc, yc, 높이가 h인 건물이 있다고 가정합니다. 건물의 중심 좌표는 모르지만 x, y 좌표와 고도 값 a를 포함하는 n개의 정보가 제공됩니다. 좌표(x, y)의 고도는 (h - |x - xc| - |y - yc|, 0)의 최대값입니다. 건물의 중심 좌표와 높이를 알아야 합니다. 좌표 xi는 배열 x에, yi는 teg 배열 y에, ai는 배열 a에 주어집니다.
따라서 입력이 n =3, x ={3, 3, 2}, y ={4, 2, 3}, a ={6, 6, 6}인 경우 출력은 3 3 7이 됩니다.
중심 좌표는 3,3이고 건물 높이는 7입니다.
단계
이 문제를 해결하기 위해 다음 단계를 따릅니다. −
check := true for initialize xc := 0, when xc <= 100, update (increase xc by 1), do: for initialize yc := 0, when yc <= 100, update (increase yc by 1), do: check := true mh := 2000000000 h := -1 for initialize i := 0, when i < n, update (increase i by 1), do: k := |(x[i] - xc) + |y[i] - yc|| if a[i] is same as 0, then: mh := minimum of mh and k else: if h < 0, then: h := a[i] + k otherwise when h is not equal to a[i] + k, then: check := false Come out from the loop if h > mh, then: check := false Ignore following part, skip to the next iteration if check is non-zero, then: Come out from the loop if check is non-zero, then: Come out from the loop print(xc, yc, h)
예시
이해를 돕기 위해 다음 구현을 살펴보겠습니다. −
#include <bits/stdc++.h>
using namespace std;
void solve(int n, vector<int> x, vector<int> y, vector<int> a){
bool check = true;
int xc, yc, h;
for (xc = 0; xc <= 100; xc++) {
for (yc = 0; yc <= 100; yc++) {
check = true;
int k, mh = 2e9;
h = -1;
for(int i = 0; i < n; i++) {
k = abs(x[i] - xc) + abs(y[i] - yc);
if (a[i] == 0) {
mh = min(mh, k);
} else {
if (h < 0) {
h = a[i] + k;
} else if (h != a[i] + k) {
check = false;
break;
}
}
}
if (h > mh) {
check = false;
continue;
}
if (check) {
break;
}
}
if (check) {
break;
}
}
cout << xc << " " << yc << " " << h;
}
int main() {
int n = 3;
vector<int> x = {3, 3, 2}, y = {4, 2, 3}, a = {6, 6, 6};
solve(n, x, y, a);
return 0;
} 입력
3, {3, 3, 2}, {4, 2, 3}, {6, 6, 6} 출력
3 3 7