Out of Boundary Paths

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 by crossing the grid boundary. You can apply at most maxMove moves to the ball.

Given the five integers m, n, maxMove, startRow, and 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 10^9 + 7.

Example 1
Inputm = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output6
There are 6 paths that move the ball out of the 2 x 2 grid using at most 2 moves from the top-left cell.
Example 2
Inputm = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output12
There are 12 paths that move the ball out of the 1 x 3 grid using at most 3 moves from the middle cell.

Constraints

  • 1 <= m, n <= 50
  • 0 <= maxMove <= 50
  • 0 <= startRow < m
  • 0 <= startColumn < n

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate