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

5-seongwon030 #23

Merged
merged 2 commits into from
Apr 2, 2024
Merged

5-seongwon030 #23

merged 2 commits into from
Apr 2, 2024

Conversation

seongwon030
Copy link
Collaborator

πŸ”— 문제 링크

점프점프

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

2μ‹œκ°„

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

  1. ν˜„μž¬ μΈλ±μŠ€μ™€ 점프 횟수λ₯Ό 큐에 λ„£λŠ”λ‹€.
  2. λ°©λ¬Έ μ—¬λΆ€ 체크λ₯Ό μœ„ν•œ 리슀트λ₯Ό λ§Œλ“ λ‹€.
  3. 큐에 μš”μ†Œκ°€ μžˆμ„ λ•Œ κΉŒμ§€
    1. ν˜„μž¬ μΈλ±μŠ€μ™€ μ ν”„μΈλ±μŠ€ νŠœν”Œμ„ νμ—μ„œ κΊΌλ‚Έλ‹€.
    2. λ§Œμ•½ ν˜„μž¬ μΈλ±μŠ€κ°€ λ§ˆμ§€λ§‰ 칸에 ν•΄λ‹Ήν•˜λ©΄ jump 좜λ ₯ ν›„ μ’…λ£Œν•œλ‹€.
    3. μ•„λ‹ˆλ©΄ k 리슀트 μ•ˆμ˜ 숫자 μ΄ν•˜λ§ŒνΌ λ°˜λ³΅ν•œλ‹€
    4. λ‹€μŒ 인덱슀λ₯Ό λ°©λ¬Έν•˜μ§€ μ•Šμ€ μƒνƒœλ©΄ μ ν”„νšŸμˆ˜μ— 1을 λ”ν•˜κ³  큐에 μΆ”κ°€ ν›„ 방문처리
  4. exit μ•ˆ ν•˜λ©΄ -1 좜λ ₯
from collections import deque

n = int(input())
k = list(map(int,input().split()))

cur, jump = 0,0
queue = deque()
queue.append((cur,jump))  # ν˜„μž¬ 인덱슀, 점프 인덱슀
visited = [] 

while queue:
  cur, jump = queue.popleft()
  if cur == len(k)-1:   # ν˜„μž¬ μΈλ±μŠ€κ°€ λ§ˆμ§€λ§‰ 칸에 ν•΄λ‹Ήν•˜λ©΄ jump좜λ ₯ ν›„ μ’…λ£Œ
    print(jump)
    exit()

  for i in range(1, k[cur]+1):  # kμ•ˆμ— μžˆλŠ” 숫자 μ΄ν•˜λ§ŒνΌ 였λ₯Έμͺ½μœΌλ‘œ 점프 κ°€λŠ₯
    next = cur + i 
    if next not in visited:
      queue.append((next, jump+1))
      visited.append(next)

print(-1)

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

bfs 둜 ν’€μ—ˆλŠ”λ° dpλ¬Έμ œμ˜€λ‹€.. dp 둜 λ‹€μ‹œ 풀어보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€

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.

λ”± 처음 λ΄€μ„λ•Œ DPλ¬Έμ œλ„€? 라고 μƒκ°ν–ˆλŠ”λ° BFS둜 μ ‘κ·Όλ˜μ–΄ μžˆμ–΄μ„œ 쑰건을 ν™•μΈν•΄λ³΄λ‹ˆ 1000 μ΄ν•˜λ©΄ κ°€λŠ₯ν•  것? 같기도 ν•˜κ³  생각이 λ“€λ”κ΅°μš”...
μ• μ΄ˆμ— ν•œ 쀄이라 μ–΄λ–»κ²Œ ν’€λ“  상관없닀고 λ“€κΈ΄ ν–ˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ„œλŠ” μ ν”„λ‘œ 갈 수 μžˆλŠ” ν•œμΉΈ ν•œμΉΈ λ°©λ¬Έν–ˆλŠ”μ§€ μ²΄ν¬ν•˜λ©° κ°”λ„€μš”.
쀑간에 μž‘μ€ 값이 λ“€μ–΄μ˜€λ©΄ μ–΄λ–‘ν•˜μ§€ μƒκ°ν–ˆλŠ”λ° μ• μ΄ˆμ— μ„ μž…μ„ μΆœ νŠΉμ„±μ΄λΌ λ¨Όμ € λΉ μ Έλ‚˜κ°€λŠ” 것은 ν˜„μž¬λ³΄λ‹€ μž‘μ€ 값이기에 상관없닀고 λ°”λ‘œ κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€!

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.

κΈ°μ™• μ΄λ ‡κ²Œ ν‘Όκ±° DP둜 ν’€μ—ˆμ„ λ•Œλž‘ BFS둜 ν’€μ—ˆμ„ λ•Œ 횟수 차이도 μ •λ¦¬ν•΄μ£Όμ…¨μœΌλ©΄ μ’‹κ² μ–΄μš”!
그리고 μ§€κΈˆμ€ queueμ—μ„œ popν•œ cur이 정닡인지 ν™•μΈν•˜λŠ” 둜직이라면 ν˜Ήμ‹œ queue에 λ„£κΈ° 전에 νŒλ‹¨ν•΄μ„œ 탐색을 μ’…λ£Œν•˜λŠ” λ°©λ²•μœΌλ‘œλŠ” ν’€ 수 μ—†μ„κΉŒμš”?

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.

저도 각 μΉΈλ§ˆλ‹€ μ΅œμ†Œ μ ν”„νšŸμˆ˜λ₯Ό μ €μž₯ν•˜λŠ” 배열을 μ΄μš©ν•˜μ—¬ dpλ°©μ‹μœΌλ‘œ ν’€ 쀄 μ•Œμ•˜λŠ”λ° bfsλ‘œλ„ ν’€μˆ˜ μžˆκ΅°μš”.. ν˜Ήμ‹œ λ‚˜μ€‘μ— dpλ°©μ‹μœΌλ‘œλ„ 풀어보신닀면 두 λ°©μ‹μ˜ μ‹œκ°„λ³΅μž‘λ„ 차이λ₯Ό μ•Œ 수 μžˆμ„κΉŒμš”..?

@seongwon030
Copy link
Collaborator Author

κΈ°μ™• μ΄λ ‡κ²Œ ν‘Όκ±° DP둜 ν’€μ—ˆμ„ λ•Œλž‘ BFS둜 ν’€μ—ˆμ„ λ•Œ 횟수 차이도 μ •λ¦¬ν•΄μ£Όμ…¨μœΌλ©΄ μ’‹κ² μ–΄μš”! 그리고 μ§€κΈˆμ€ queueμ—μ„œ popν•œ cur이 정닡인지 ν™•μΈν•˜λŠ” 둜직이라면 ν˜Ήμ‹œ queue에 λ„£κΈ° 전에 νŒλ‹¨ν•΄μ„œ 탐색을 μ’…λ£Œν•˜λŠ” λ°©λ²•μœΌλ‘œλŠ” ν’€ 수 μ—†μ„κΉŒμš”?

for i in range(1, k[cur] + 1):  # kμ•ˆμ— μžˆλŠ” 숫자 μ΄ν•˜λ§ŒνΌ 였λ₯Έμͺ½μœΌλ‘œ 점프 κ°€λŠ₯
        next = cur + i
        if next not in visited:
            visited.append(next)
            if next == len(k) - 1:  # λ‹€μŒ μΈλ±μŠ€κ°€ λ§ˆμ§€λ§‰ 칸이면 jump좜λ ₯ ν›„ μ’…λ£Œ
                print(jump + 1)
                exit()
            queue.append((next, jump + 1))

큐에 λ„£κΈ° 전에 λ°”λ‘œ ν™•μΈν•˜λ‹ˆκΉŒ μ‹œκ°„λ³΅μž‘λ„κ°€ κ°œμ„ λ˜λ„€μš”! dp둜 λ‹€μ‹œ 풀어보고 리뷰 λ‚¨κΈΈκ²Œμš©

@seongwon030 seongwon030 merged commit 0d350e8 into main Apr 2, 2024
@seongwon030 seongwon030 deleted the 5-seongwon030 branch April 2, 2024 02:00
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