-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
30-xxubin04 #113
30-xxubin04 #113
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ²μ κ³ λ―Όμ΄λΌκ³ νμ ¨λ κ² μ΄λ° λ§μμ΄μ ¨κ΅°μ! μ λ μμ§ BFSλ DFSμ λν΄ μ μμ§ λͺ»ν΄μ λμμ λ리긴 μ΄λ ΅μ§λ§.. ν¨κ» λ¬λ €λ΄ μλ€!!π μκ³ νμ ¨μ΄μπ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ°μ μμ ν(νμ΄μ¬μμλ heapq)λ₯Ό μ¬μ©ν΄μ νΌ μ½λκ° μλλΌκ΅¬μ?
λ΄ μ μ(s)μ μλ μ μ(t)μ μ°¨μ΄κ° κ°μ₯ μμ κ±Έ topμ μ¬λ¦¬λ μλ¦¬λ‘ heapqλ₯Ό μ°λ€μ...
from heapq import heappush, heappop
input = open(0).readline
for _ in range(C := int(input())):
S, T = map(int, input().split())
heap = [(0, S, T)] # μ μ μ°¨μ΄, λ΄ μ μ, μλ μ μ
while heap:
cnt, s, t = heappop(heap) # μ μ μ°¨μ΄κ° μμ μμΌλ‘ topμ μ¬λΌμ΄
if s == t:
print(cnt)
break
for ns, nt in [(s * 2, t + 3), (s + 1, t)]:
if ns > nt:
continue
heappush(heap, (cnt + 1, ns, nt))
if t == o: | ||
ans.append(cnt) | ||
return ans |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whileλ¬Έμμ μ²μμΌλ‘ t == o
μ 걸리λ κ²½μ°λ μμ°μ€λ½κ² μ΅λ¨ κ²½λ‘ μ
λλ€. κ·Έκ²μ΄ bfsμ΄κΈ° λλ¬Έμ΄μ£ .
κ·Έλ κΈ° λλ¬Έμ 리μ€νΈμ λ£μ νμ μμ΄ μ¦μ cntλ₯Ό λ°ννλ κ²μΌλ‘λ μΆ©λΆν©λλ€.
if t == 0:
return cnt
elif t * 2 <= o + 3: | ||
q.append((cnt + 1, t * 2, o + 3)) | ||
q.append((cnt + 1, t + 1, o)) | ||
else: | ||
q.append((cnt + 1, t + 1, o)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λΆλΆμ μ΄λ κ² νλ€κΈ° 보λ¨, μ΄μ°¨νΌ t * 2 μΌμ΄μ€μ t + 1 μΌμ΄μ€ λͺ¨λ κ³ λ €ν΄λ΄μΌ νμμμ?
κ·Έλ λ€λ©΄ elif - else λ³΄λ¨ κ°κ° ifλ‘ κ±Έμ΄λ²λ¦¬λ κ²μ΄ λ«μ΅λλ€. μμ 쑰건문μ μνν κ²κ³Ό λ³κ°λ‘ μλ 쑰건문λ νμΈν΄λ΄μΌ νλκΉμ :)
if t * 2 <= o + 3:
q.append((cnt + 1, t * 2, o + 3))
if t + 1 <= o:
q.append((cnt + 1, t + 1, o))
for i in range(n := int(input())): | ||
ans = [] | ||
tae, opp = map(int, input().split()) | ||
print(min(BFS(0, tae, opp))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
첫 λ²μ§Έ μ½λ©νΈμμ cntλ₯Ό μ¦μ λ°νλ°μμΌλ, μ¬κΈ°μλ κ·Έ κ°μ μΆλ ₯λ§ νλ©΄ λκ² μ£ ?
print(BFS(0, tae, opp))
π λ¬Έμ λ§ν¬
λ°±μ€ 14562λ²: νκΆμππ»ββοΈ
βοΈ μμλ μκ°
1μκ° 30λΆ?
β¨ μλ μ½λ
1. λ¬Έμ μ΄ν΄
νκ· μ΄μ μ μμ μλμ μ μκ° μ£Όμ΄μ§ λ, λ μ¬λμ μ μκ° λμΌν΄μ§λλ‘ νλ μ°μ λ°μ°¨κΈ°μ μ΅μ νμλ₯Ό ꡬνλ κ²μ΄λ€.
2. DFS? BFS?
μ²μμ λ¬Έμ λ₯Ό λ³΄κ³ DFSμ BFS μ€ λ¬΄μμΌλ‘ νμ΄μΌ ν μ§ κ°μ μ‘μ§ λͺ»νλ€. κ·Έμ κΉλ¨Ήμλ보λ€... κ·Έλμ DFSλ‘ λμ νλ€κ° μ무리 μ½λλ₯Ό μμ ν΄λ³΄μλ μνλ κ°μ΄ λμ€μ§ μμ BFSλ‘ λ€μ μ½λλ₯Ό μ§°λ€. BFS μκ³ λ¦¬μ¦μ΄ κΈ°μ΅μ΄ μ μλ λΈλ‘κ·Έμμ μμ΄λμ΄λ₯Ό μ°Έκ³ νμλ€.
BFSλ λλΉ μ°μ νμμ΄λ€. μ¦, λ λ Έλ μ¬μ΄μ μ΅λ¨ κ²½λ‘λ₯Ό ꡬνκ±°λ μμμ κ²½λ‘λ₯Ό μ°Ύκ³ μΆμ λ μ΄ λ°©λ²μ ννλ€. μ΄ λ¬Έμ λ μμμ κ²½λ‘λ₯Ό μ°Ύκ³ μΆμ λμ μ μ¬νλ€. νκ· μ΄μ μ²μμ μ μμμ μ΄λ ν μ°μ λ°μ°¨κΈ°λ₯Ό ννκ² λλ κ²½λ‘μ λ°λΌ κ°μ΄ λ¬λΌμ§κΈ° λλ¬Έμ΄λ€.
λ°λλ‘, DFSλ κΉμ΄ μ°μ νμμ΄λ€. λͺ¨λ λ Έλλ₯Ό λ°©λ¬Ένκ³ μ νλ κ²½μ°μ μ΄ λ°©λ²μ ννλ€. DFSλ λ€μ λΈλμΉλ₯Ό νμνκΈ° μ μ ν΄λΉ λΈλμΉλ₯Ό μλ²½νκ² νμνκ³ λμ΄κ°λ λ°©λ²μ΄λ©°, μκΈ° μμ μ νΈμΆνλ μν μκ³ λ¦¬μ¦μ΄λ€.
3. μ½λ λΆμ
BFSμ νΉμ§μ dequeλ₯Ό μ¬μ©νλ κ²μ΄λ€. κ·Έλ¬λ―λ‘ collections λͺ¨λμμ dequeλ₯Ό importν΄μ€λ€.
c
: μ΄μ μ°μ λ°μ°¨κΈ° κ³Όμ μμμ cnt 맀κ°λ³μ |cnt
x
: νκ· μ΄μ νμ¬ μ μ 맀κ°λ³μ |t
y
: μλμ νμ¬ μ μ 맀κ°λ³μ |o
ans
: 'νκ· μ μ == μλ μ μ'κ° λλ μ¬λ¬ κ²½λ‘μμμ cntκ° λͺ¨μΈ 리μ€νΈνκ· μ μ
==μλ μ μ
νκ· μ μ
* 2 <=μλ μ μ + 3
λμμ¬ μ μλ κ°μ 건λ κ²μ΄λ€.νκ· μ μ + 1
κ°μλ μ μ
λ³΄λ€ λ¬΄μ‘°κ±΄ μκ±°λ κ°μ κ²½μ°μ΄λ€. (νκ· μ μ + 1
<=μλ μ μ
) κ·Έλ¬λ―λ‘, μμνκ· μ μ * 2
<=μλ μ μ + 3
μ λ κ°μ§ μ°μ λ°μ°¨κΈ°λ₯Ό ν΄μ£Όλ κ²½λ‘λ₯Ό λ€ κ³ λ €ν΄μΌ νλ€.νκ· μ μ
<=μλ μ μ
<= 100)μΌλ‘ μ£Όμ΄μ‘μΌλ―λ‘ νκ· μ μκ° μλ μ μλ³΄λ€ μ»€μ§λ κ²½μ°λ μκ°νμ§ μμλ λλ€.n
λ²λ§νΌ μ°μ λ°μ°¨κΈ°μ μ΅μ νμλ₯Ό ꡬν΄μ£Όμ΄μΌ νλ―λ‘ forλ¬Έ μμans = []
λ₯Ό μ¨μ£Όμ΄ μλ‘μ΄ νκΆλ 겨루기λ₯Ό ν λλ§λ€ ansκ° κ°±μ λλλ‘ ν΄μΌνλ€.μμμ ꡬνν BFSν¨μλ ansλ₯Ό λ°ννλ―λ‘
min(BFS(0, tae, opp))
==min(ans)
μ΄λ€. κ·Έλ¬λ―λ‘, μ¬λ¬ κ²½μ°μ cntλ€μ λ΄κ³ μλ ans 리μ€νΈμμ μ΅μκ°μ μΆλ ₯νλ€.4. μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©
BFSμ DFSλ₯Ό λ€μ μ΅νμ΅λλ€.
κΉλ¨Ήμ§ μλλ‘ κ³μ νμ΄μΌκ² μ΅λλ€..π