https://www.acmicpc.net/problem/25558
25558번: 내비게이션
1번 내비게이션이 안내한 경로는 $(0,0) \rightarrow (11,1) \rightarrow (9,9) \rightarrow (10,10)$으로, 총 거리는 $12 + 10 + 2 = 24$이다. 2번 내비게이션이 안내한 경로는 $(0,0) \rightarrow (1,12) \rightarrow (9,9) \ri
www.acmicpc.net
풀이 과정
시작점과 도착점, 그리고 각 내비게이션마다 방문해야 하는 중간점 정보를 담고 있을 때, 중간점을 모두 방문하고 도착점에 도착하는 거리가 가장 짧은, 가장 좋은 내비게이션이 무엇인지 구하는 문제이다.
그냥 문제가 시키는대로 구현하면 된다. 각 내비게이션마다 시작점 -> 중간점들 -> 도착점 경로의 맨해튼 거리를 모두 구해주고 그 중 가장 작은 거리를 안내하는 내비게이션의 번호를 출력하면 된다.
소스 코드
import sys
def manhatton(a, b, c, d):
return abs(a-c) + abs(b-d)
N = int(sys.stdin.readline())
sx, sy, ex, ey = map(int, sys.stdin.readline().split())
answer = []
for _ in range(N):
distance = 0
now_x, now_y = sx, sy
Mi = int(sys.stdin.readline())
for _ in range(Mi):
next_x, next_y = map(int, sys.stdin.readline().split())
distance += manhatton(now_x, now_y, next_x, next_y)
now_x, now_y = next_x, next_y
distance += manhatton(now_x, now_y, ex, ey)
answer.append(distance)
answer_index = 0
min_answer = min(answer)
for index, value in enumerate(answer):
if value == min_answer:
min_answer = value
answer_index = index + 1
print(answer_index)
'알고리즘 문제 풀이 > 백준' 카테고리의 다른 글
백준 25565 - 딸기와 토마토 [Python] (0) | 2022.09.10 |
---|---|
백준 25562 - 차의 개수 [Python] (0) | 2022.09.07 |
백준 24542 - 튜터-튜티 관계의 수 [Python] (0) | 2022.08.16 |
백준 10026 - 적록색약 [C++] (0) | 2022.08.01 |
백준 1012 - 유기농 배추 [C++] (0) | 2022.08.01 |