Skip to content
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

Merged
merged 2 commits into from
Feb 26, 2024
Merged

30-xxubin04 #113

merged 2 commits into from
Feb 26, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Feb 22, 2024

πŸ”— 문제 링크

λ°±μ€€ 14562번: νƒœκΆŒμ™•πŸ‹πŸ»β€β™‚οΈ


βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ 30λΆ„?


✨ μˆ˜λ„ μ½”λ“œ

1. 문제 이해

νƒœκ· μ΄μ˜ 연속 발차기 2가지

  1. νƒœκ· μ΄μ˜ 점수 * 2μ΄λ©΄μ„œ μƒλŒ€μ˜ 점수 + 3
  2. νƒœκ· μ΄μ˜ 점수 + 1

νƒœκ· μ΄μ˜ μ μˆ˜μ™€ μƒλŒ€μ˜ μ μˆ˜κ°€ μ£Όμ–΄μ§ˆ λ•Œ, 두 μ‚¬λžŒμ˜ μ μˆ˜κ°€ 동일해지도둝 ν•˜λŠ” 연속 발차기의 μ΅œμ†Œ 횟수λ₯Ό κ΅¬ν•˜λŠ” 것이닀.



2. DFS? BFS?

μ²˜μŒμ— 문제λ₯Ό 보고 DFS와 BFS 쀑 λ¬΄μ—‡μœΌλ‘œ ν’€μ–΄μ•Ό 할지 감을 μž‘μ§€ λͺ»ν–ˆλ‹€. κ·Έμƒˆ κΉŒλ¨Ήμ—ˆλ‚˜λ³΄λ‹€... κ·Έλž˜μ„œ DFS둜 λ„μ „ν•˜λ‹€κ°€ 아무리 μ½”λ“œλ₯Ό μˆ˜μ •ν•΄λ³΄μ•„λ„ μ›ν•˜λŠ” 값이 λ‚˜μ˜€μ§ˆ μ•Šμ•„ BFS둜 λ‹€μ‹œ μ½”λ“œλ₯Ό μ§°λ‹€. BFS μ•Œκ³ λ¦¬μ¦˜μ΄ 기얡이 잘 μ•ˆλ‚˜ λΈ”λ‘œκ·Έμ—μ„œ 아이디어λ₯Ό μ°Έκ³ ν•˜μ˜€λ‹€.

BFSλŠ” λ„ˆλΉ„ μš°μ„  탐색이닀. 즉, 두 λ…Έλ“œ μ‚¬μ΄μ˜ μ΅œλ‹¨ 경둜λ₯Ό κ΅¬ν•˜κ±°λ‚˜ μž„μ˜μ˜ 경둜λ₯Ό μ°Ύκ³  싢을 λ•Œ 이 방법을 νƒν•œλ‹€. 이 λ¬Έμ œλŠ” μž„μ˜μ˜ 경둜λ₯Ό μ°Ύκ³  싢을 λ•Œμ™€ μœ μ‚¬ν•˜λ‹€. νƒœκ· μ΄μ˜ 처음의 μ μˆ˜μ—μ„œ μ–΄λ– ν•œ 연속 발차기λ₯Ό νƒν•˜κ²Œ λ˜λŠ” κ²½λ‘œμ— 따라 값이 달라지기 λ•Œλ¬Έμ΄λ‹€.

λ°˜λŒ€λ‘œ, DFSλŠ” 깊이 μš°μ„  탐색이닀. λͺ¨λ“  λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜κ³ μž ν•˜λŠ” κ²½μš°μ— 이 방법을 νƒν•œλ‹€. DFSλŠ” λ‹€μŒ 브랜치λ₯Ό νƒμƒ‰ν•˜κΈ° 전에 ν•΄λ‹Ή 브랜치λ₯Ό μ™„λ²½ν•˜κ²Œ νƒμƒ‰ν•˜κ³  λ„˜μ–΄κ°€λŠ” 방법이며, 자기 μžμ‹ μ„ ν˜ΈμΆœν•˜λŠ” μˆœν™˜ μ•Œκ³ λ¦¬μ¦˜μ΄λ‹€.



3. μ½”λ“œ 뢄석

from collections import deque
input = open(0).readline 

def BFS(c, x, y):
    global ans
    q = deque()
    q.append((c, x, y))
    while q:
        cnt, t, o = q.popleft()
        if t == o:
            ans.append(cnt)
            return ans
        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))

BFS의 νŠΉμ§•μ€ dequeλ₯Ό μ‚¬μš©ν•˜λŠ” 것이닀. κ·ΈλŸ¬λ―€λ‘œ collections λͺ¨λ“ˆμ—μ„œ dequeλ₯Ό importν•΄μ€€λ‹€.

c : 이전 연속 발차기 κ³Όμ •μ—μ„œμ˜ cnt λ§€κ°œλ³€μˆ˜ | cnt
x : νƒœκ· μ΄μ˜ ν˜„μž¬ 점수 λ§€κ°œλ³€μˆ˜ | t
y : μƒλŒ€μ˜ ν˜„μž¬ 점수 λ§€κ°œλ³€μˆ˜ | o
ans : 'νƒœκ·  점수 == μƒλŒ€ 점수'κ°€ λ˜λŠ” μ—¬λŸ¬ κ²½λ‘œμ—μ„œμ˜ cntκ°€ λͺ¨μΈ 리슀트

  • νƒœκ·  점수 == μƒλŒ€ 점수
    • ans에 cntλ₯Ό μΆ”κ°€ν•΄μ£Όκ³  ansλ₯Ό λ°˜ν™˜ν•œλ‹€.
  • νƒœκ·  점수 * 2 <= μƒλŒ€ 점수 + 3
    • μš°λ¦¬λŠ” νƒœκ·  μ μˆ˜μ™€ μƒλŒ€ μ μˆ˜κ°€ 같아지도둝 ν•΄μ•Όν•œλ‹€. ν•˜μ§€λ§Œ, νƒœκ·  μ μˆ˜κ°€ μƒλŒ€ μ μˆ˜λ³΄λ‹€ λ†’μ•„μ§€κ²Œ λ˜λŠ” κ²½μš°κ°€ 단 ν•œλ²ˆμ΄λΌλ„ 있으면 μ ˆλŒ€ κ°™μ•„μ§ˆ 수 μ—†λ‹€. λŒμ•„μ˜¬ 수 μ—†λŠ” 강을 κ±΄λ„Œ 것이닀.
    • μœ„μ—μ„œ μ–ΈκΈ‰ν•œ 첫 번째 연속 발차기λ₯Ό ν•˜κ²Œ λ˜μ—ˆμ„ λ•Œ, νƒœκ·  μ μˆ˜κ°€ μƒλŒ€ μ μˆ˜λ³΄λ‹€ μž‘κ±°λ‚˜ 같은 κ²½μš°μ—λ§Œ 첫 번째 연속 발차기λ₯Ό ν•΄μ€€λ‹€. 그리고 첫 번째 연속 발차기λ₯Ό ν•  수 μžˆλŠ” κ²½μš°λŠ” νƒœκ·  점수 + 1κ°€ μƒλŒ€ μ μˆ˜λ³΄λ‹€ 무쑰건 μž‘κ±°λ‚˜ 같은 κ²½μš°μ΄λ‹€. (νƒœκ·  점수 + 1 <= μƒλŒ€ 점수) κ·ΈλŸ¬λ―€λ‘œ, μœ„μ˜ νƒœκ·  점수 * 2 <= μƒλŒ€ 점수 + 3에 두 가지 연속 발차기λ₯Ό ν•΄μ£ΌλŠ” 경둜λ₯Ό λ‹€ κ³ λ €ν•΄μ•Ό ν•œλ‹€.
  • κ·Έ μ™Έμ˜ κ²½μš°λŠ” 두 번째 연속 발차기만 해쀄 수 μžˆλ‹€.
    • λ¬Έμ œμ—μ„œ (1 <= νƒœκ·  점수 <= μƒλŒ€ 점수 <= 100)으둜 μ£Όμ–΄μ‘ŒμœΌλ―€λ‘œ νƒœκ·  μ μˆ˜κ°€ μƒλŒ€ μ μˆ˜λ³΄λ‹€ μ»€μ§€λŠ” κ²½μš°λŠ” μƒκ°ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.


for i in range(n := int(input())):
    ans = []
    tae, opp = map(int, input().split())
    print(min(BFS(0, tae, opp)))

n번만큼 연속 발차기의 μ΅œμ†Œ 횟수λ₯Ό ꡬ해주어야 ν•˜λ―€λ‘œ forλ¬Έ μ•ˆμ— ans = []λ₯Ό 써주어 μƒˆλ‘œμš΄ νƒœκΆŒλ„ 겨루기λ₯Ό ν•  λ•Œλ§ˆλ‹€ ansκ°€ κ°±μ‹ λ˜λ„λ‘ ν•΄μ•Όν•œλ‹€.

μœ„μ—μ„œ κ΅¬ν˜„ν•œ BFSν•¨μˆ˜λŠ” ansλ₯Ό λ°˜ν™˜ν•˜λ―€λ‘œ min(BFS(0, tae, opp)) == min(ans)이닀. κ·ΈλŸ¬λ―€λ‘œ, μ—¬λŸ¬ 경우의 cnt듀을 λ‹΄κ³  μžˆλŠ” ans λ¦¬μŠ€νŠΈμ—μ„œ μ΅œμ†Ÿκ°’μ„ 좜λ ₯ν•œλ‹€.



4. 전체 μ½”λ“œ

from collections import deque
input = open(0).readline 

def BFS(c, x, y):
    global ans
    q = deque()
    q.append((c, x, y))
    while q:
        cnt, t, o = q.popleft()
        if t == o:
            ans.append(cnt)
            return ans
        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))
    
for i in range(n := int(input())):
    ans = []
    tae, opp = map(int, input().split())
    print(min(BFS(0, tae, opp)))

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

BFS와 DFSλ₯Ό λ‹€μ‹œ μ΅ν˜”μŠ΅λ‹ˆλ‹€.
κΉŒλ¨Ήμ§€ μ•Šλ„λ‘ 계속 ν’€μ–΄μ•Όκ² μŠ΅λ‹ˆλ‹€..πŸ˜‚


Copy link
Collaborator

@Dolchae Dolchae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ €λ²ˆμ— 고민이라고 ν•˜μ…¨λ˜ 게 이런 λ§μ”€μ΄μ…¨κ΅°μš”! 저도 아직 BFSλ‚˜ DFS에 λŒ€ν•΄ 잘 μ•Œμ§€ λͺ»ν•΄μ„œ 도움을 λ“œλ¦¬κΈ΄ μ–΄λ ΅μ§€λ§Œ.. ν•¨κ»˜ λ‹¬λ €λ΄…μ‹œλ‹€!!πŸ˜„ μˆ˜κ³ ν•˜μ…¨μ–΄μš”πŸ˜Š

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a 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))

Comment on lines +12 to +14
if t == o:
ans.append(cnt)
return ans
Copy link
Collaborator

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

Comment on lines +15 to +19
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))
Copy link
Collaborator

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)))
Copy link
Collaborator

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))

@xxubin04 xxubin04 merged commit 513981e into AlgoLeadMe:main Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants