Skip to content

Commit b77804d

Browse files
committed
Day17
1 parent dcae264 commit b77804d

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

leetcode3/최민지/576.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
3+
MOD = 10**9 + 7
4+
memo = {}
5+
6+
def dfs(r, c, moves):
7+
# 경계 밖으로 나간 경우 -> 성공 경로 1개 반환
8+
if r < 0 or r >= m or c < 0 or c >= n:
9+
return 1
10+
# 이동 횟수를 다 썼는데 여전히 경계 안인 경우 -> 실패 0개 반환
11+
if moves == 0:
12+
return 0
13+
# 이미 계산한 결과가 있는 경우
14+
if (r, c, moves) in memo:
15+
return memo[(r, c, moves)]
16+
17+
# 상, 하, 좌, 우 4방향으로 탐색
18+
paths = (
19+
dfs(r - 1, c, moves - 1) +
20+
dfs(r + 1, c, moves - 1) +
21+
dfs(r, c - 1, moves - 1) +
22+
dfs(r, c + 1, moves - 1)
23+
) % MOD
24+
25+
memo[(r, c, moves)] = paths
26+
return memo[(r, c, moves)]
27+
28+
return dfs(startRow, startColumn, maxMove)
29+

0 commit comments

Comments
 (0)