🎯 알고리즘 과제 - 평면좌표계에서 가장 가까운 좌표쌍 찾기
🤔 나의 풀이
📌 문제
- 알고리즘 과제 > 평면좌표계에서 가장 가까운 좌표쌍 찾기
📌 날짜
2020.04.15
📌 시도 횟수
Failed
💡 Code
def solution(pair):
if len(pair) < 2:
return format(0, ".2f")
px = sorted(pair)
py = sorted(pair, key=lambda x: x[1])
answer = closest_pair(px, py)
return format(answer, ".2f")
def closest_pair(px, py):
if len(px) <= 3:
return brute_force(px)
mid = len(px) // 2
pxL = px[:mid]
pyL = [p for p in py if p[0] < px[mid][0]]
pxR = px[mid:]
pyR = [p for p in py if p[0] >= px[mid][0]]
delta = min(closest_pair(pxL, pyL), closest_pair(pxR, pyR))
return closest_split_pair(px, py, delta)
def closest_split_pair(px, py, delta):
mid = len(px) // 2
median_X = px[mid][0]
candidates = []
for p in py:
if abs(p[0] - median_X) < delta:
candidates.append(p)
for i in range(len(candidates) - 1):
for j in range(i + 1, min(i + 8, len(candidates))):
dist = get_distance(candidates[i], candidates[j])
delta = min(delta, dist)
return delta
def get_distance(p1, p2):
x1, y1 = p1[0], p1[1]
x2, y2 = p2[0], p2[1]
return math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
def brute_force(px):
min_dist = float("inf")
for i in range(len(px) - 1):
for j in range(i + 1, len(px)):
min_dist = min(min_dist, get_distance(px[i], px[j]))
return min_dist
for _ in range(int(input())):
pair_num = int(input())
arr = list(map(int, input().split()))
pairs = []
for i in range(0, len(arr), 2):
py = arr[i]
px = arr[i + 1]
pairs.append([px, py])
print(solution(pairs))
💡 문제 해결 방법
- 정렬된 x에 대하여 왼/오른쪽 부분에서 최소 거리를 각각 찾는다.
- 해당 왼/오른쪽의 범위 안에 있는 좌표들을 y를 기준으로 정렬한 후
- delta = (왼쪽 최소 거리, 오른쪽 최소 거리 중 더 작은 값) 라고 했을 때, 중심축으로부터의 거리가 delta 값보다 작은 값들 중..
현재 좌표로부터 최대 7개를 비교하여 최소 거리를 조사한다.
- 이렇게 구한 최소 거리를 delta로 갱신하고 다시 1의 결과로 리턴 후 반복...
- 따라서 이러한 과정(1 ~ 4)의 반복을 통해 최종적으로 가장 가까운 좌표쌍의 거리(delta)를 구할 수 있다.
💡 새롭게 알게 된 점
- 어렵당ㅠ