๋ฌธ์
https://www.acmicpc.net/problem/1520
ํ์ด
# ๋ฐฑ์ค 1520๋ฒ ๋ฌธ์ - ๋ด๋ฆฌ๋ง ๊ธธ
from collections import deque
dx, dy = [0,0,-1,1], [1,-1,0,0]
m, n = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(m)]
dp = [[-1]*n for _ in range(m)]
cnt = 0
def dfs(x, y):
if x == m-1 and y == n-1:
return 1
if dp[x][y] == -1:
dp[x][y] = 0
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < m and 0 <= ny < n:
if graph[x][y] > graph[nx][ny]:
dp[x][y] += dfs(nx,ny)
return dp[x][y]
print(dfs(0,0))
๋งจ ์ฒ์ ๋จ์ํ DFS ๋ฅผ ์ฌ์ฉํ๋๋ฐ ์ด๋ ๊ฒ ๋๋ฉด 500*500 ์ ์ฌ๊ท์ด๊ธฐ ๋๋ฌธ์ ์๊ฐ์ด๊ณผ๊ฐ ๋๋ค.
๋ฐ๋ผ์ ๋ฐฉ๋ฌธํ๋ ๊ณณ์ ๋ค์ ์ฒ๋ฆฌํ์ง ์์์ผ ํ๋๋ฐ ๊ทธ๊ฒ์ dpํ ์ด๋ธ์ ๋ง๋ค์ด ํด๊ฒฐํ์๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 2447๋ฒ_๋ณ ์ฐ๊ธฐ - 10 (0) | 2023.04.21 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 7662๋ฒ_์ด์ค ์ฐ์ ์์ ํ (0) | 2023.04.20 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver1] 1991๋ฒ_ํธ๋ฆฌ ์ํ (0) | 2023.04.19 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver3] 15654๋ฒ_N๊ณผM(5) (0) | 2023.04.18 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold3] 2206๋ฒ_๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ (0) | 2023.04.17 |