๋ฌธ์
https://www.acmicpc.net/problem/11060
11060๋ฒ: ์ ํ ์ ํ
์ฌํ์ด๊ฐ 1×N ํฌ๊ธฐ์ ๋ฏธ๋ก์ ๊ฐํ์๋ค. ๋ฏธ๋ก๋ 1×1 ํฌ๊ธฐ์ ์นธ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ , ๊ฐ ์นธ์๋ ์ ์๊ฐ ํ๋ ์ฐ์ฌ ์๋ค. i๋ฒ์งธ ์นธ์ ์ฐ์ฌ ์๋ ์๋ฅผ Ai๋ผ๊ณ ํ์ ๋, ์ฌํ์ด๋ Ai์ดํ๋งํผ ์ค๋ฅธ์ชฝ์ผ๋ก
www.acmicpc.net
ํ์ด
# ๋ฐฑ์ค 11060๋ฒ ๋ฌธ์ - ์ ํ ์ ํ
import collections
n=int(input())
arr=list(map(int, input().split()))
dp=[1e9 for _ in range(n)]
def bfs():
q=collections.deque()
q.append((0, 0))
dp[0]=0
while q:
cur, cnt=q.popleft()
if arr[cur]==0:
continue
for i in range(1, arr[cur]+1):
nxt=cur+i
if 0<=nxt<n and dp[nxt]>cnt+1:
dp[nxt]=cnt+1
q.append((nxt, cnt+1))
bfs()
if dp[n-1]==1e9:
dp[n-1]=-1
print(dp[n-1])
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] 11052๋ฒ_์นด๋ ๊ตฌ๋งคํ๊ธฐ 2 (1) | 2022.12.02 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] 17086๋ฒ_์๊ธฐ ์์ด 2 (0) | 2022.12.02 |
๐ฉ [๋ฐฑ์ค] [Python] 2210๋ฒ_์ซ์ํ ์ ํ (0) | 2022.11.28 |
๐ฉ [๋ฐฑ์ค] [Python] 5567๋ฒ_๊ฒฐํผ์ (0) | 2022.11.28 |
๐ฉ [๋ฐฑ์ค] [Python] 11055๋ฒ_๊ฐ์ฅ ํฐ ์ฆ๊ฐ ๋ถ๋ถ ์์ด (0) | 2022.11.28 |