λ¬Έμ
λ°±μ€ μ¨λΌμΈ μ μ§ - 14226λ²
νμ΄ κ³Όμ
μνν μ μλ μ°μ°μ μ’
λ₯κ° 3κ°μ§ μΌ λ, μ΄λͺ¨ν°μ½ 1κ°μμ N
κ°λ‘ λ§λ€κΈ° μν μ΅μ μ°μ°μ νμλ₯Ό ꡬνλ λ¬Έμ μ
λλ€.
νμ¬ μνλ₯Ό λνλ΄λ λ³μλ νλ©΄μ μ‘΄μ¬νλ μ΄λͺ¨ν°μ½μ μ
μ ν΄λ¦½λ³΄λμ μλ μ΄λͺ¨ν°μ½μ μ
μ΄λ―λ‘
μ΄ λ λ³μλ₯Ό μν곡μ°μ 맀κ°λ³μλ‘ μ ν©λλ€.
μ£Όμν μ μ ν΄λ¦½λ³΄λμ μ μ₯λ μ΄λͺ¨ν°μ½μ νλ©΄μ λΆμ¬λ£κΈ° νλ€κ³ ν΄λ¦½λ³΄λμμ μμ λμ§λ μλλ€λ κ²μ
λλ€.
μ½λ
from collections import deque
S = int(input())
def bfs():
q = deque()
visit = [[0] * (2 * S + 1) for _ in range(2 * S + 1)]
q.append([1, 0, 0])
visit[1][0] = 1
while q:
here, clipboard, time = q.popleft()
if here == S:
return time
# νλ©΄μ μλ μ΄λͺ¨ν°μ½ μ€ νλ μμ
if here > 0 and not visit[here - 1][clipboard]:
visit[here - 1][clipboard] = 1
q.append([here - 1, clipboard, time + 1])
# ν΄λ¦½λ³΄λμ μλ λͺ¨λ μ΄λͺ¨ν°μ½ νλ©΄μ λΆμ¬λ£κΈ°
if clipboard and here + clipboard <= 2 * S and not visit[here + clipboard][clipboard]:
visit[here + clipboard][clipboard] = 1
q.append([here + clipboard, clipboard, time + 1])
# νλ©΄μ μλ μ΄λͺ¨ν°μ½ 볡μ¬ν΄μ ν΄λ¦½λ³΄λμ μ μ₯
if here > 0 and here != clipboard and not visit[here][here]:
visit[here][here] = 1
q.append([here, here, time + 1])
def solution():
answer = bfs()
return answer
print(solution())
λ°μν
'π algorithm > boj' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
BOJ 18242 - λ€λͺ¨λ€λͺ¨ μλ ₯κ²μ¬ (0) | 2021.03.08 |
---|---|
BOJ 9184 - μ λλ ν¨μ μ€ν (0) | 2021.03.08 |
BOJ 2529 - λΆλ±νΈ (0) | 2021.03.08 |
BOJ 1802 - μ’ μ΄ μ κΈ° (0) | 2021.03.08 |
BOJ 17829 - 222-νλ§ (0) | 2021.03.08 |
π¬ λκΈ