Out of Boundary Paths - Problem
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 crossing the grid boundary). You can apply at most maxMove moves to the ball.
Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.
Input & Output
Example 1 — Small Grid
$
Input:
m = 2, n = 3, maxMove = 2, startRow = 1, startColumn = 1
›
Output:
6
💡 Note:
From center position (1,1), the ball can exit the boundary in 6 different ways within 2 moves: moving up-up, up-left, up-right, down-down, down-left, down-right
Example 2 — Single Move
$
Input:
m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
›
Output:
12
💡 Note:
From middle of 1x3 grid, ball can exit left or right in 1 move (2 ways), or make more complex paths using all 3 moves for total of 12 paths
Example 3 — No Moves
$
Input:
m = 2, n = 2, maxMove = 0, startRow = 0, startColumn = 0
›
Output:
0
💡 Note:
With 0 moves allowed, the ball cannot move at all, so there are 0 paths to exit the boundary
Constraints
- 1 ≤ m, n ≤ 50
- 0 ≤ maxMove ≤ 50
- 0 ≤ startRow < m
- 0 ≤ startColumn < n
Visualization
Tap to expand
Understanding the Visualization
1
Input
2×3 grid, start at (1,1), max 2 moves
2
Process
Try all 4 directions, count boundary exits
3
Output
Total of 6 different paths to exit
Key Takeaway
🎯 Key Insight: Use DP to systematically count all possible boundary-crossing paths
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code