Skip to content

Commit f1e3dd2

Browse files
authored
Create 576. Out of Boundary Paths.py
1 parent 1f36161 commit f1e3dd2

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
3+
'''
4+
1. 아이디어 :
5+
-
6+
7+
2. 시간복잡도 :
8+
O(maxMove * n * m * 4)
9+
10+
3. 자료구조/알고리즘 :
11+
dp
12+
13+
'''
14+
class Solution:
15+
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
16+
MOD = 1_000_000_009
17+
18+
dp = [[0] * n for _ in range(m)]
19+
dp[startRow][startColumn] = 1
20+
21+
ans = 0
22+
dir = [0,1],[0,-1],[-1,0],[1,0]
23+
24+
for _ in range(maxMove):
25+
next_dp = [[0] * n for _ in range(m)]
26+
27+
for row in range(m):
28+
for col in range(n):
29+
if dp[row][col] == 0:
30+
continue
31+
32+
for dx, dy in dir:
33+
next_row = row + dx
34+
next_col = col + dy
35+
36+
if 0<=next_row<m and 0<=next_col<n:
37+
next_dp[next_row][next_col] += dp[row][col]
38+
next_dp[next_row][next_col] %= MOD
39+
else:
40+
ans += dp[row][col]
41+
ans %= MOD
42+
dp = next_dp
43+
44+
return ans

0 commit comments

Comments
 (0)