1/18 Coding Test

๊น€ํƒœ์ค€ยท2024๋…„ 1์›” 22์ผ
0

Coding Test - BOJ

๋ชฉ๋ก ๋ณด๊ธฐ
52/64
post-thumbnail

โœ… Coding Test

๐ŸŽˆ 1810 ์ง•๊ฒ€๋‹ค๋ฆฌ ๋‹ฌ๋ฆฌ๊ธฐ 2

์ง•๊ฒ€๋‹ค๋ฆฌ ์œ„๋ฅผ ๋‹ฌ๋ฆฌ๋Š” ๊ฒฝ๊ธฐ๋ฅผ ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

์ง•๊ฒ€๋‹ค๋ฆฌ๊ฐ€ ๋†“์—ฌ ์žˆ๋Š” ์œ„์น˜๋Š” (x,y) ์ˆœ์„œ์Œ์œผ๋กœ ํ‘œํ˜„๋˜๊ณ  ์‹œ์ž‘์ ์€ ์–ธ์ œ๋‚˜ (0,0) ์›์ ์ด๋‹ค.
๊ฒฝ๊ธฐ๊ฐ€ ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ํ˜„์žฌ ์œ„์น˜ํ•œ ์ง•๊ฒ€๋‹ค๋ฆฌ์™€ x์ขŒํ‘œ ์ฐจ์ด๊ฐ€ 2์ดํ•˜์ด๊ณ  y์ขŒํ‘œ ์ฐจ์ด๋„ 2์ดํ•˜์ธ ์ง•๊ฒ€๋‹ค๋ฆฌ๋กœ๋งŒ ์ ํ”„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ์Šน์„ ์€ x์ถ•์— ํ‰ํ–‰ํ•œ ์ง์„ ์ธ๋ฐ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฒฐ์Šน์„ ๊ณผ y์ขŒํ‘œ๊ฐ€ ๋™์ผํ•œ ์ง•๊ฒ€๋‹ค๋ฆฌ์— ๋„๋‹ฌํ•˜๋ฉด ๊ฒฐ์Šน์„ ์„ ํ†ต๊ณผํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๊ฒฝ๊ธฐ๊ฐ€ ๋๋‚œ๋‹ค.

์ง•๊ฒ€๋‹ค๋ฆฌ๋“ค์˜ ์œ„์น˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๊ฒฝ๊ธฐ์—์„œ ์Šน๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒฝ๋กœ๋ž€ ๊ฒฝ๋กœ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ๋Š” ์ ํ”„๋“ค์˜ ๊ธธ์ด ํ•ฉ์ด ์ตœ์†Œ์ธ ๊ฒฝ๋กœ์ด๋‹ค. (x1, y1)์— ์œ„์น˜ํ•œ ์ง•๊ฒ€๋‹ค๋ฆฌ์—์„œ (x2, y2)์— ์œ„์น˜ํ•œ ์ง•๊ฒ€๋‹ค๋ฆฌ๋กœ์˜ ์ ํ”„๋Š” ์œ ํด๋ฆฌ๋””์•ˆ ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค.

์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ง•๊ฒ€๋‹ค๋ฆฌ ์ˆ˜ n, ๊ฒฐ์Šน์„  y์ขŒํ‘œ F๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ n๊ฐœ์˜ ์ง•๊ฒ€๋‹ค๋ฆฌ๋“ค์˜ ์ขŒํ‘œ๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. n์˜ ๋ฒ”์œ„๋Š” [0, 50,000], F๋‚˜ ๊ฐ ์ขŒํ‘œ๋“ค์˜ ๋ฒ”์œ„๋Š” [0, 1,000,000]์ผ ๋•Œ ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒฝ๋กœ์˜ ๊ธธ์ด๋ฅผ ์†Œ์ˆ˜ ์ฒซ์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•ด ์ •์ˆ˜๋กœ ์ถœ๋ ฅํ•˜๋„๋ก ํ•œ๋‹ค.๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

import sys, math, heapq
input = sys.stdin.readline

n, f = map(int, input().split())
# ์ง•๊ฒ€๋‹ค๋ฆฌ ๋ณ„ ์ขŒํ‘œ, ์ธ๋ฑ์Šค ์ €์žฅ
location = [(0, 0, 0)]
# ์ง•๊ฒ€๋‹ค๋ฆฌ์—์„œ ์ด๋™ ๊ฐ€๋Šฅํ•œ ํƒ€ ๋‹ค๋ฆฌ ๋ฒˆํ˜ธ ์ธ๋ฑ์Šค, ๊ฑฐ๋ฆฌ ์ €์žฅ
graph = [[] for _ in range(n+1)]
# ๋ชฉํ‘œ์ง€์  ์ธ๋ฑ์Šค
target = []
for i in range(1, n+1):
    x, y = map(int ,input().split())
    location.append((x, y, i))
    if y == f:
        target.append(i)
visited = set()
# x์ขŒํ‘œ ์ด๋™ ํ™•์ธ
location.sort(key = lambda x: x[0])
for i in range(n+1):
    x1, y1, idx1 = location[i]
    for j in range(n+1):
        x2, y2, idx2 = location[j]
        if abs(x1-x2) > 2:
            break
        elif abs(x1-x2) <= 2 and abs(y1-y2) <= 2:
            dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)
            graph[idx1].append((idx2, dist))
            graph[idx2].append((idx1, dist))
            if idx1 < idx2:
                visited.add((idx1, idx2))
            else:
                visited.add((idx2, idx1))
# y์ขŒํ‘œ ๋น„๊ต
location.sort(key = lambda x: x[1])
for i in range(n+1):
    x1, y1, idx1 = location[i]
    for j in range(n+1):
        x2, y2, idx2 = location[j]
        if abs(y1-y2) > 2:
            break
        elif abs(x1-x2) <= 2 and abs(y1-y2) <= 2:
            if idx1 < idx2 and (idx1, idx2) in visited:
                continue
            elif idx1 > idx2 and (idx2, idx1) in visited:
                continue
            dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)
            graph[idx1].append((idx2, dist))
            graph[idx2].append((idx1, dist))


def dijkstra():
    INF = int(1e9)
    distance = [INF for _ in range(n+1)]
    distance[0] = 0
    heap = []
    heapq.heappush(heap, (0, 0))
    while heap:
        now_dist, now_node = heapq.heappop(heap)
        if distance[now_node] < now_dist:
            continue
        for next_node, next_dist in graph[now_node]:
            if distance[next_node] > now_dist + next_dist:
                distance[next_node] = now_dist + next_dist
                heapq.heappush(heap, (now_dist + next_dist, next_node))
    answer = INF
    for i in target:
        answer = min(answer, distance[i])
    if answer == INF:
        return -1
    else:
        return round(answer)
print(dijkstra())

< ํ•ด์„ค >

์šฐ์„ ์ ์œผ๋กœ ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ๋‹ด์„ ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ํ•„์š”ํ•˜๊ณ , ์ด๋ฅผ ํƒ์ƒ‰๋งˆ๋‹ค ๋งค๋ฒˆ ๋น„๊ตํ•ด์ค„ ์ž‘์—…์ด ํ•„์š”ํ•˜๊ธฐ์— deque ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•ด ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•ด์•ผ ๊ฒ ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๊ธฐ์— BFS, DFS๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
๋”ฐ๋ผ์„œ ๋งค ํƒ์ƒ‰๋งˆ๋‹ค Greedy/Back-Trackingํ•œ ๋ฐฉ๋ฒ•์ด ํ•„์š”ํ•œ๋ฐ..
์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ..
(์˜ค๋‹ต)

profile
To be a DataScientist

0๊ฐœ์˜ ๋Œ“๊ธ€