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 #22

Closed
wants to merge 1 commit into from
Closed

5-seongwon030 #22

wants to merge 1 commit into from

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 둜 λ‹€μ‹œ 풀어보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant