알고리즘/소스코드
KOREATECH 1041: 최소 이동거리 구하기 - 2차원
cjw.git
2021. 3. 2. 21:48
Link : judge.koreatech.ac.kr/problem.php?id=1041
Python
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
from sys import stdin
data_length = int(stdin.readline().strip())
info = list()
for i in range(data_length):
info.append(list(map(int, stdin.readline().strip().split(' '))))
t_x, t_y = -1, -1
info.sort(key=lambda x: x[0]) # x축을 기준으로 정렬
if len(info) % 2 == 0: # 짝수
t_x = (info[len(info) // 2 - 1][0] + info[len(info) // 2][0]) // 2
else: # 홀수
t_x = info[len(info) // 2][0]
info.sort(key=lambda x: x[1]) # x축을 기준으로 정렬
if len(info) % 2 == 0: # 짝수
t_y = (info[len(info) // 2 - 1][1] + info[len(info) // 2][1]) // 2
else: # 홀수
t_y = info[len(info) // 2][1]
cal = 0
for i in info:
cal += abs(i[0] - t_x) + abs(i[1] - t_y)
print(cal)
|
cs |
FeedBack
cjw.git@gmail.com