576. Out of Boundary Paths
Medium
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12Last updated
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12Last updated
class Solution:
def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
return self.dfs(m,n, startRow, startColumn, maxMove) % (10**9 + 7)
@lru_cache(None)
def dfs(self, m, n , row, col, movesLeft):
if ((row < 0 or row >= m) or (col < 0 or col >= n)) and movesLeft >= 0:
return 1
if movesLeft < 0:
return 0
return self.dfs(m,n,row-1,col, movesLeft-1) + self.dfs(m,n,row,col+1, movesLeft-1) + self.dfs(m,n,row+1,col, movesLeft-1) + self.dfs(m,n,row,col-1, movesLeft-1)