File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+ sys .setrecursionlimit (10 ** 7 )
3+
4+ m , n = map (int , input ().split ()) # m: 세로, n: 가로
5+ Map = [list (map (int , sys .stdin .readline ().split ())) for _ in range (m )]
6+ # DP table 구현
7+ vst = [[- 1 ]* n for _ in range (m )]
8+
9+ def dfs (x , y ): # x: row, y: col
10+
11+ dx = [0 , 0 , - 1 , 1 ]
12+ dy = [1 , - 1 , 0 , 0 ]
13+
14+ # 도착 지점에 도달하면 1(한 가지 경우의 수)를 리턴
15+ if x == (m - 1 ) and y == (n - 1 ):
16+ return 1
17+
18+ # 이미 방문한 적이 있다면 그 위치에서 출발하는 경우의 수를 리턴
19+ if vst [x ][y ] != - 1 :
20+ return vst [x ][y ]
21+
22+ cnt = 0
23+ for i in range (4 ):
24+ nx = x + dx [i ]
25+ ny = y + dy [i ]
26+ # 탐색 범위를 벗어나는 경우 pass
27+ if nx < 0 or ny < 0 or nx > (m - 1 ) or ny > (n - 1 ): continue
28+ # 내리막 일 경우 (현재 길 < 이전 길)
29+ if Map [nx ][ny ] < Map [x ][y ]:
30+ cnt += dfs (nx , ny )
31+
32+ vst [x ][y ] = cnt
33+ return vst [x ][y ]
34+
35+ print (dfs (0 , 0 ))
You can’t perform that action at this time.
0 commit comments