Skip to content

Commit 694186e

Browse files
committed
[BOJ] #1520.내리막길 / 골드3 / 50(∆)
1 parent acf3255 commit 694186e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
sys.setrecursionlimit(10**6)
3+
input = sys.stdin.readline
4+
5+
# 방향 벡터 (상, 하, 좌, 우)
6+
dx = [-1, 1, 0, 0]
7+
dy = [0, 0, -1, 1]
8+
9+
def dfs(x, y):
10+
# 도착점에 도달하면 경로 1개 반환
11+
if x == M - 1 and y == N - 1:
12+
return 1
13+
14+
# 이미 방문한 적이 있다면 저장된 경로 개수 반환
15+
if dp[x][y] != -1:
16+
return dp[x][y]
17+
18+
# 현재 위치에서 가능한 경로 개수 계산
19+
dp[x][y] = 0 # 초기화
20+
for i in range(4):
21+
nx, ny = x + dx[i], y + dy[i]
22+
if 0 <= nx < M and 0 <= ny < N and graph[nx][ny] < graph[x][y]: # 내리막길 조건
23+
dp[x][y] += dfs(nx, ny)
24+
25+
return dp[x][y]
26+
27+
# 입력 처리
28+
M, N = map(int, input().split())
29+
graph = [list(map(int, input().split())) for _ in range(M)]
30+
31+
# DP 배열 (-1로 초기화)
32+
dp = [[-1] * N for _ in range(M)]
33+
34+
H = dfs(0, 0)
35+
print(H)

0 commit comments

Comments
 (0)