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

4-seongwon030 #16

Merged
merged 2 commits into from
Mar 26, 2024
Merged

4-seongwon030 #16

merged 2 commits into from
Mar 26, 2024

Conversation

seongwon030
Copy link
Collaborator

@seongwon030 seongwon030 commented Mar 25, 2024

πŸ”— 문제 링크

μˆ¨λ°”κΌ­μ§ˆ3

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

2μ‹œκ°„

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

처음 풀이

from collections import deque

result = 0
MAX = 10**5 # 움직일 수 μžˆλŠ” μ΅œλŒ€μ’Œν‘œ
dist = [0] * (MAX + 1) # ν•΄λ‹Ή μœ„μΉ˜μ— λ„μ°©ν–ˆμ„ λ•Œ μ‹œκ°„
n, k = map(int,input().split())

q = deque()
q.append(n)

while q:
    x = q.popleft()
    if x==k: # xλ³€μˆ˜μΈ 수빈의 μœ„μΉ˜κ°€ 동생이 μžˆλŠ” k와 같을 λ•Œ 멈좀
      result = dist[x]
      break
    # nx = 4,6,10 (ν˜„μž¬ μœ„μΉ˜ 5일 λ•Œ 이동할 수 μžˆλŠ” λ°©ν–₯)
    for nx in (x*2, x-1, x+1):
      # λ²”μœ„λ‚΄μ— 있고 아직 λ°©λ¬Έμ•ˆν–ˆλ‹€λ©΄
      if 0 <= nx <= MAX and (not dist[nx] or dist[nx] == dist[x]+1):
        if nx == x*2: # κ³±ν•˜κΈ°2λ₯Ό ν•œ μ’Œν‘œλΆ€ν„° 체크
          dist[nx] = dist[x]
        else:
          dist[nx] = dist[x] + 1 
        q.append(nx)
print(result)

μˆ¨λ°”κΌ­μ§ˆ2μ—μ„œ 쑰금 λ³€ν˜•ν•΄μ„œ μ’Œν‘œκ°€ x*2인 κ²½μš°λΆ€ν„° 쑰건을 달고
ν•΄λ‹Ή μ’Œν‘œλŠ” 0μ΄ˆμ΄λ―€λ‘œ μ’Œν‘œμ΄λ™μ€ 없도둝 μ„€μ •ν•œλ‹€. κ³±ν•˜κΈ° 2에 μš°μ„ μˆœμœ„λ₯Ό λ‘” μ΄μœ λŠ” λ‚˜λ¨Έμ§€λ³΄λ‹€ 더 빨리 kμ’Œν‘œμ— 도착할 κ°€λŠ₯성이 λ†’κΈ° λ•Œλ¬Έμ΄λ‹€. λ‚˜λ¨Έμ§€ +1κ³Ό -1λŠ” κΈ°μ‘΄ μ’Œν‘œμ—μ„œ +1을 ν•΄μ£Όμ—ˆλ‹€. 그리고 λ‹€μ‹œ 큐에 μΆ”κ°€ν•˜λ©΄μ„œ xκ°€ k와 같을 λ•Œ λ©ˆμΆ”λ„λ‘ν–ˆλ‹€.

ν‹€λ¦° 이유

κ³±ν•˜κΈ° 2인 경우 μ’Œν‘œκ°€ λ‹€μ‹œ 0이 λ˜λŠ” κ²½μš°κ°€ 생긴닀. -> 방문을 ν–ˆμœΌλ‚˜ dist[nx]κ°€ μ—¬μ „νžˆ 0이 λœλ‹€λŠ” λœ»μ΄λ‹€.

고친 풀이

from collections import deque
import sys

input = sys.stdin.readline

result = 0
MAX = 10**5 # 움직일 수 μžˆλŠ” μ΅œλŒ€μ’Œν‘œ
dist = [-1] * (MAX + 1) # ν•΄λ‹Ή μœ„μΉ˜μ— λ„μ°©ν–ˆμ„ λ•Œ μ‹œκ°„
n, k = map(int,input().split())

q = deque()
q.append(n)
dist[n] = 0 # μ‹œμž‘μœ„μΉ˜λ₯Ό μ²΄ν¬ν•˜κΈ° μœ„ν•΄ 0으둜 μ„€μ •
while q:
    x = q.popleft()
    if x==k: # xλ³€μˆ˜μΈ 수빈의 μœ„μΉ˜κ°€ 동생이 μžˆλŠ” k와 같을 λ•Œ 멈좀
      result = dist[x]
      break
    # nx = 4,6,10 (ν˜„μž¬ μœ„μΉ˜ 5일 λ•Œ 이동할 수 μžˆλŠ” λ°©ν–₯)
    for nx in (x*2, x-1, x+1):
      # λ²”μœ„λ‚΄μ— 있고 아직 λ°©λ¬Έμ•ˆν–ˆλ‹€λ©΄
      if 0<=nx<=MAX and dist[nx] == -1: 
        if nx == x*2: # κ³±ν•˜κΈ°2λ₯Ό ν•œ μ’Œν‘œλΆ€ν„° 체크
          dist[nx] = dist[x]
        else:
          dist[nx] = dist[x] + 1 
        q.append(nx)
print(result)

λ°©λ¬Έν•œ 경우 ν˜Όλ™μ΄ 생기지 μ•Šλ„λ‘ dist의 배열을 λͺ¨λ‘ 0이 μ•„λ‹Œ -1둜 μ„€μ •ν•˜κ³  μ‹œμž‘μœ„μΉ˜λ₯Ό μ²΄ν¬ν•˜κΈ° μœ„ν•΄ dist[n]은 0으둜 μ„€μ •ν–ˆλ‹€. λ°‘μ˜ μ‘°κ±΄λ¬Έμ—μ„œλŠ” λ²”μœ„ μ•ˆμ— 있으며 ν•΄λ‹Ή μ’Œν‘œκ°€ -1일 λ•Œ (λ°©λ¬Έν•˜μ§€ μ•Šμ•˜λ‹€λŠ” 뜻) 둜 λ°”κΎΈμ–΄ μ£Όμ—ˆλ‹€.

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

λ°©λ¬Έν•˜μ§€ μ•ŠμŒμ„ λ‚˜νƒ€λ‚΄κΈ° μœ„ν•œ λ°°μ—΄μ˜ 값을 λͺ¨λ‘ 0으둜 μ„€μ •ν–ˆμ„ λ•Œ, μ†Œμš” μ‹œκ°„μ΄ μ—†λŠ” 경우λ₯Ό μ²˜λ¦¬ν•˜μ§€ λͺ»ν•˜λ―€λ‘œ -1둜 λ°”κΎΈμ–΄μ„œ ν•΄κ²°ν•  수 μžˆμŒμ„ μ•Œ 수 μžˆμ—ˆλ‹€.

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

문제 링크가 잘λͺ»λœκ±° κ°™μŠ΅λ‹ˆλ‹€~
μ‹œκ°„μ΄ 0초인 μ˜ˆμ™ΈλΆ€λΆ„μ„ 잘 μž‘μ•„μ£Όμ…¨λ˜ 것 κ°™λ„€μš”~
λ°©λ¬Έ 배열을 ν•˜λ‚˜λ₯Ό μž‘μ•„μ£Όμ—ˆμ„ 경우 더 직관적일 μˆ˜λ„ μžˆμ§€λ§Œ ν•˜λ‚˜μ˜ λ°°μ—΄μ—μ„œ λ°©λ¬Έν–ˆλ˜ ν‘œμ‹œλ₯Ό -1둜 기쀀을 μž‘μ•„λ‘ μœΌλ‘œμ¨ λ©”λͺ¨λ¦¬μ μœΌλ‘œλ„ μ’€ 더 μ‹ κ²½ μ¨μ€„μˆ˜ μžˆμ—ˆλ˜ 점이 μ’‹μ•˜μŠ΅λ‹ˆλ‹€~

@seongwon030
Copy link
Collaborator Author

문제 링크가 잘λͺ»λœκ±° κ°™μŠ΅λ‹ˆλ‹€~ μ‹œκ°„μ΄ 0초인 μ˜ˆμ™ΈλΆ€λΆ„μ„ 잘 μž‘μ•„μ£Όμ…¨λ˜ 것 κ°™λ„€μš”~ λ°©λ¬Έ 배열을 ν•˜λ‚˜λ₯Ό μž‘μ•„μ£Όμ—ˆμ„ 경우 더 직관적일 μˆ˜λ„ μžˆμ§€λ§Œ ν•˜λ‚˜μ˜ λ°°μ—΄μ—μ„œ λ°©λ¬Έν–ˆλ˜ ν‘œμ‹œλ₯Ό -1둜 기쀀을 μž‘μ•„λ‘ μœΌλ‘œμ¨ λ©”λͺ¨λ¦¬μ μœΌλ‘œλ„ μ’€ 더 μ‹ κ²½ μ¨μ€„μˆ˜ μžˆμ—ˆλ˜ 점이 μ’‹μ•˜μŠ΅λ‹ˆλ‹€~

문제링크 μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹Ή κ°μ‚¬ν•©λ‹ˆλ‹€

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

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

λ°©λ¬Έν•˜μ§€μ•Šμ•˜μ„κ²½μš°λ₯Ό 0으둜 λ‘λŠ” μ‹€μˆ˜λ₯Ό μ£Όμ˜ν•΄μ•Όκ² λ„€μš” 이번 prλ„κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€ !

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

BFS 문제만 ν‘Έμ‹œλ”λ‹ˆ 큐 μ‚¬μš© μ‹€λ ₯이 점점 μ’‹μ•„μ§€μ‹œλ„€μš”. λ‘œμ§λ„ 직관적이고 주석도 친절히 λ‹¬μ•„μ£Όμ…”μ„œ μ΄ν•΄ν•˜κΈ° μ‰¬μ› μŠ΅λ‹ˆλ‹€ :)

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.

4 participants