Out of Boundary Paths

A ball sits on cell `(startRow, startColumn)` of an `m x n` grid. In one move it can roll to any of the four adjacent cells, and it may roll off the grid. Using at most `maxMove` moves, count the paths that end with the ball leaving the grid. The count can be huge, so return it modulo $10^9 + 7$.

30:00

Out of Boundary Paths
medium
Free written solution
Topics
Companies

A ball sits on cell (startRow, startColumn) of an m x n grid. In one move it can roll to any of the four adjacent cells, and it may roll off the grid. Using at most maxMove moves, count the paths that end with the ball leaving the grid. The count can be huge, so return it modulo 109+710^9 + 7.

Example 1:
Input: {"m":2,"n":2,"maxMove":2,"startRow":0,"startColumn":0}
Output: 6
Constraints:
  • 1≤m,n≤501 \leq m, n \leq 50

  • 0≤maxMove≤500 \leq \text{maxMove} \leq 50

  • 0≤startRow<m0 \leq \text{startRow} < m

  • 0≤startColumn<n0 \leq \text{startColumn} < n

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"m":2,"n":2,"maxMove":2,"startRow":0,"startColumn":0}

moves used

0 / 2

left this move

–

count (answer)

0

Paths standing on each cell

0

0

0

0

■ start (0, 0)■ paths on grid: 0

Lay out a 2 x 2 table where each cell will hold how many paths are standing on it after the current number of moves.

Variables
VariableValue
m2
n2
maxMove2
DepthFunction Call
Stack empty
0/6