Skip to content

Commit 753ed64

Browse files
Merge pull request #1841 from CodingTestStudy2/남효정
[남효정] Day17
2 parents e8e64d9 + 0137a3f commit 753ed64

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 풀이 실패
2+
class Solution:
3+
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
4+
memo = {}
5+
MOD = 10**9 + 7
6+
7+
def dp(r, c, moves):
8+
# 밖으로 탈출한 경우
9+
if r < 0 or r >= m or c < 0 or c >= n:
10+
return 1
11+
12+
# 이동 횟수 전부 사용한 경우
13+
if moves == 0:
14+
return 0
15+
16+
# 이미 계산된 위치거나 남은 이동 횟수라면 저장된 값으로 사용
17+
if (r, c, moves) in memo:
18+
return memo[(r, c, moves)]
19+
20+
# 상, 하, 좌, 우 이동 경로 합산
21+
res = (
22+
dp(r + 1, c, moves - 1)
23+
+ dp(r - 1, c, moves - 1)
24+
+ dp(r, c + 1, moves - 1)
25+
+ dp(r, c - 1, moves - 1)
26+
) % MOD
27+
28+
# 결과 기록 후 반환
29+
memo[(r, c, moves)] = res
30+
return res
31+
32+
return dp(startRow, startColumn, maxMove)

0 commit comments

Comments
 (0)