There is an m x n
grid with a ball. The ball is initially at the position (startRow, startColumn)
. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid boundary) in one step. You can move the ball up, down, left, or right. Calculate the number of paths to move the ball out of grid boundary in exactly maxMove
steps. Since the answer can be very large, return it modulo 10^9 + 7
.
Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6
Explanation: There are 6 ways to move the ball out of boundary in 2 moves.
m
and n
be?
We can solve this problem using dynamic programming. The idea is to use a 3D DP table dp[i][j][k]
where:
i
and j
represent the cell position.k
is the number of moves left.Each cell will keep track of the number of ways to reach out of the boundary from that point with k
moves left. The base condition is when k = 0
, indicating no moves left and thus zero out-bound paths from any cell.
We’ll iterate through the grid for each move, updating the DP table by aggregating the possible steps from the current cell to neighboring cells and checking boundary conditions.
def findPaths(m, n, maxMove, startRow, startColumn):
MODULO = 10**9 + 7
# Initialize the DP table
dp = [[[0 for _ in range(maxMove + 1)] for _ in range(n + 2)] for _ in range(m + 2)]
# Increment all boundaries
for i in range(1, m + 1):
dp[i][0][0] = 1
dp[i][n + 1][0] = 1
for j in range(1, n + 1):
dp[0][j][0] = 1
dp[m + 1][j][0] = 1
for moves in range(1, maxMove + 1):
for i in range(1, m + 1):
for j in range(1, n + 1):
dp[i][j][moves] = (dp[i - 1][j][moves - 1] + dp[i + 1][j][moves - 1] +
dp[i][j - 1][moves - 1] + dp[i][j + 1][moves - 1]) % MODULO
return sum(dp[startRow + 1][startColumn + 1][moves] for moves in range(1, maxMove + 1)) % MODULO
# Example usage:
m = 2
n = 2
maxMove = 2
startRow = 0
startColumn = 0
print(findPaths(m, n, maxMove, startRow, startColumn)) # Output: 6
(m + 2) x (n + 2) x (maxMove + 1)
to store the number of paths for cells including boundaries, initialized to zero.(i, j)
and each number of moves remaining k
, sum the paths from the four adjacent positions with one less move.maxMove
steps from the start position.O(m * n * maxMove)
because we iterate over each cell and each remaining move in the grid multiple times.O(m * n * maxMove)
due to the DP table.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?