[BOJ][๐ŸŸก4][๋ฐฑ์ค€#02158] ์‚ฐ์•…์ž์ „๊ฑฐ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2158


๋ฌธ์ œ

๋‹คํ•ด๋Š” ํ‰์†Œ ์‚ฐ์•…์ž์ „๊ฑฐ ํƒ€๊ธฐ๋ฅผ ์ฆ๊ธด๋‹ค. ๋‹คํ•ด๋Š” ์˜ค๋žœ ์‹œ๊ฐ„๋™์•ˆ ์šด๋™์„ ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์•„ํ•˜์ง€๋งŒ, ๋•Œ๋กœ๋Š” ๋˜๋„๋ก ์งง์€ ์‹œ๊ฐ„๋™์•ˆ ์‚ฐ์•…์ž์ „๊ฑฐ ํƒ€๊ธฐ๋ฅผ ๋งˆ์น˜๊ธฐ๋ฅผ ์›ํ•˜๊ธฐ๋„ ํ•œ๋‹ค. ์‚ฐ์˜ ๋ชจ์Šต์€ R(1 โ‰ค R โ‰ค 100)ํ–‰ C(1 โ‰ค C โ‰ค 100)์—ด์˜ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„๋œ๋‹ค. ํ–‰๋ ฌ์˜ ๊ฐ ์ˆ˜๋Š” ๊ทธ ์œ„์น˜์˜ ๋†’์ด(๊ณ ๋„)๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. ๋‹คํ•ด๋Š” ํ˜„์žฌ ํ–‰๋ ฌ์˜ (1, 1) ์œ„์น˜์— ์žˆ๊ณ , (R, C) ์œ„์น˜๋กœ ์ด๋™ํ•˜๋ ค ํ•œ๋‹ค. ๋‹คํ•ด์˜ (1, 1)์œ„์น˜์—์„œ์˜ ์ดˆ๊ธฐ ์†๋„๋Š” V(1 โ‰ค V โ‰ค 1,000,000)์ด๋‹ค. ํ–‰๋ ฌ์—์„œ์˜ ์›€์ง์ž„์€ ์œ„, ์•„๋ž˜, ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์˜ ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•˜์ž. ์†๋„๊ฐ€ V๋ผ๋Š” ์˜๋ฏธ๋Š” ๋‹จ์œ„์‹œ๊ฐ„๋™์•ˆ V์นธ์„ ์›€์ง์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์ž์ „๊ฑฐ๋ฅผ ํƒ€๊ณ  ์ด๋™ํ•˜๋‹ค๋ณด๋ฉด ๋†’์ด ์ฐจ์ด์— ์˜ํ•ด ์†๋„๊ฐ€ ๊ฐ€์†๋˜๊ธฐ๋„ ํ•˜๊ณ  ๊ฐ์†๋˜๊ธฐ๋„ ํ•œ๋‹ค. ํ˜„์žฌ ์œ„์น˜์˜ ๋†’์ด๊ฐ€ A์ด๊ณ , ์ด๋™ํ•˜๋ ค๋Š” ์œ„์น˜์˜ ๋†’์ด๊ฐ€ B์ผ ๋•Œ, ์†๋„๋Š” ๋ฐฐ๊ฐ€ ๋œ๋‹ค(2A-Bย ๋ฐฐ). ์‹ค์ œ๋กœ๋Š” ์ด๋™ํ•˜๋Š” ๋„์ค‘์— ์†๋„๊ฐ€ ๋ณ€ํ•˜๊ฒŒ ๋˜์ง€๋งŒ, ๋ฌธ์ œ์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด์„œ ์ด๋™์„ ์™„์ „ํžˆ ๋งˆ์นœ ํ›„์— ์†๋„๊ฐ€ ๋ณ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜์ž. ์ฆ‰, ์ด๋™์„ ํ•  ๋•Œ์—๋Š” ์ฒ˜์Œ์˜ ์†๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์ด๋™ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•œ๋‹ค. ์‚ฐ์˜ ๋†’์ด์— ๋Œ€ํ•œ ์ •๋ณด์™€ ์ดˆ๊ธฐ ์†๋„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, (1, 1)์˜ ์œ„์น˜์—์„œ (R, C)์˜ ์œ„์น˜๋กœ ์ด๋™ํ•  ๋•Œ ํ•„์š”ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์„ ๊ตฌํ•ด๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์„ธ ์ •์ˆ˜ V, R, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ R๊ฐœ์˜ ์ค„์—๋Š” C๊ฐœ์˜ ์ •์ˆ˜๋กœ ํ–‰๋ ฌ์ด ์ฃผ์–ด์ง„๋‹ค. ํ–‰๋ ฌ์˜ ๊ฐ ์ˆ˜๋Š” -25 ์ด์ƒ 25 ์ดํ•˜์˜ ์ •์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ด๋™์— ํ•„์š”ํ•œ ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ ˆ๋Œ€/์ƒ๋Œ€ ์˜ค์ฐจ๋Š” 10-2๊นŒ์ง€ ํ—ˆ์šฉํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

1 2 3
2 1 2
9 9 1


์ถœ๋ ฅ

2.50


My Sol

import sys
input = sys.stdin.readline
from collections import deque

V, I, J = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(I)]
time = [[float('inf')]*J for _ in range(I)]
time[0][0] = 0

Q = deque()
Q.append((0, 0, V))
while Q:
    ni, nj, speed = Q.popleft()
    for di, dj in ((-1,0),(1,0),(0,1),(0,-1)):
        si, sj = ni+di, nj+dj
        if not (0<=si<I and 0<=sj<J): continue
        if time[si][sj] <= time[ni][nj] + (1/speed): continue
        time[si][sj] = time[ni][nj] + (1/speed)
        speed_new = speed * (2**(mat[ni][nj]-mat[si][sj]))
        Q.append((si, sj, speed_new))

print(f'{time[I-1][J-1]:.2f}')

deque๋ฅผ ์ด์šฉํ•ด ์‚ฌ๋ฐฉ์˜ time์„ ํ™•์ธํ•˜๊ณ , ๋งŒ์•ฝ ํ•ด๋‹น ์ขŒํ‘œ๊นŒ์ง€ ๋„๋‹ฌํ•˜๋Š” ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ด๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ  ๊ณ„์‚ฐ๋œ ๊ธฐ์ค€์ ๊ณผ ์†๋„๋ฅผ Q์— ๋„ฃ์–ด ์ƒˆ๋กœ์šด ๊ธฐ์ค€์ ์œผ๋กœ ์‚ผ๋Š”๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

from heapq import heappush, heappop

v, r, c = map(int, input().split())
li = [list(map(int, input().split())) for _ in range(r)]

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

heap = []
heappush(heap, (0, 0, 0, v)) # time_spent, x, y, velocity
time_spents = [[float('inf')] * c for _ in range(r)]
time_spents[0][0] = 0

while heap:
    time_spent, x, y, velocity = heappop(heap)
    
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        
        if nx < 0 or nx >= r or ny < 0 or ny >= c:
            continue
        n_time_spent = time_spent + 1 / velocity
        if n_time_spent < time_spents[nx][ny]:
            time_spents[nx][ny] = n_time_spent
            nvelocity = velocity * (2 ** (li[x][y] - li[nx][ny]))
            heappush(heap, (n_time_spent, nx, ny, nvelocity))
            
print(time_spents[-1][-1])

๊ฑฐ์˜ 10๋ฐฐ์— ๊ฐ€๊น๊ฒŒ ์—ฐ์‚ฐ์‹œ๊ฐ„์ด ์งง์€ ํ’€์ด๊ฐ€ ์žˆ์–ด์„œ ๊ฐ€์ ธ์™€ ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ๋ฐฉ์‹์€ ๋‚˜์ฒ˜๋Ÿผ time์— ๋Œ€ํ•œ 2์ฐจ์› ๋ฐฐ์—ด์— inf๋ฅผ ๋„ฃ์–ด ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๋‚˜๋ž‘ ๋‹ค๋ฅธ ๊ฒƒ์€ ๋‚˜๋Š” deque๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ด ๋ถ„์€ heap์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, visit ์ฒดํฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ๋„ ์•„๋‹Œ๋ฐ ์™œ ์ด๋ ‡๊ฒŒ ์—ฐ์‚ฐ์‹œ๊ฐ„ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š”์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค.

heap์˜ ์ •๋ ฌ์€ ์ตœ์†Œ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋˜๋Š”๋ฐ, time ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์ฒดํฌํ•ด์ฃผ๋Š” ๋ถ€๋ถ„๋„ ๋น„์Šทํ•œ๋ฐ, ์™œ ์‹œ๊ฐ„์ด ์ด๋ ‡๊ฒŒ ์ฐจ์ด๊ฐ€ ๋‚ ๊นŒ. ์–ด์ฉŒ๋ฉด if๋ฌธ์„ ํ†ต๊ณผํ•˜์ง€ ์•Š์•„ ์ œ๊ณฑ ์—ฐ์‚ฐ์ด๋‚˜ heap์— ๋“ค์–ด๊ฐ€๋Š” ์ˆ˜์˜ ์ฐจ์ด๊ฐ€ ๋‚˜์„œ์ผ ๊ฒƒ ๊ฐ™๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ