๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
-
S2 | DP
DFS๋ก ํด๋ดค์ง๋ง ์ญ์๋ ์๊ฐ์ด๊ณผ!!
์๊ฐ๋ณต์ก๋๊ฐ O(N^2) ์ธ DP๋ก ํ์ด๋ดค๋ค.
dp์๋ ํ์ฌ ์์น๊น์ง ์ฌ ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ด์์ ํด๊ฒฐํจ.
๐ป์์ค ์ฝ๋
N = int(input())
field = [list(map(int, input().split())) for _ in range(N)]
answer = 0
dp = [[0] * N for _ in range(N)] # i,j๊น์ง ์ฌ ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ ์ฅ
dp[0][0] = 1
for i in range(N):
for j in range(N):
if i == N - 1 and j == N - 1: # ๋์ ๋๋ฌํ์ ๋
print(dp[i][j])
break
cur_cnt = field[i][j]
# ์ค๋ฅธ์ชฝ์ผ๋ก ๊ฐ๊ธฐ
if j + cur_cnt < N:
dp[i][j + cur_cnt] += dp[i][j]
# ์๋๋ก ๊ฐ๊ธฐ
if i + cur_cnt < N:
dp[i + cur_cnt][j] += dp[i][j]
๐๋ฌธ์ ํ์ธ
์ถ์ฒ: BACKJOON ONLINE JUDGE
๋ฐ์ํ
'Algorithm Problem > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] ๋ฐฑ์ค - 1965. ์์ ๋ฃ๊ธฐ (0) | 2020.10.14 |
---|---|
[python] ๋ฐฑ์ค - 2504. ๊ดํธ์ ๊ฐ (0) | 2020.10.13 |
[python] ๋ฐฑ์ค - 1780. ์ข ์ด์ ๊ฐ์ (0) | 2020.10.11 |
[python] ๋ฐฑ์ค - 11279. ์ต๋ ํ (0) | 2020.10.10 |
[python] ๋ฐฑ์ค - 10819. ์ฐจ์ด๋ฅผ ์ต๋๋ก (0) | 2020.10.09 |