dp
with dimensions m x n
and set all values to 0.dp[startRow][startColumn]
to 1, as there is one way to be at the starting position with 0 moves.maxMove
, updating the dp
array for each number of moves:
a. Create a temporary 2D array temp
with dimensions m x n
and set all values to 0.
b. Iterate over each cell (i, j)
in the dp
array:
i. For each of the four directions (up, down, left, right), check if moving in that direction would go out of bounds.
ii. If it goes out of bounds, increment the count
of paths leading out.
iii. If it doesn't go out of bounds, update temp[i][j]
by adding the number of ways to reach the adjacent cell from the previous move.
c. After updating all cells, copy temp
to dp
for the next iteration.count
of paths leading out of the grid, modulo 10^9 + 7
.