Grid Paths with Override Passes
Learn this problemProblem statement
A delivery robot navigates a city represented by an n x m grid. A cell containing 1 is an open street, while a cell containing 0 is blocked by construction.
The robot starts at the top-left cell (0, 0) and must reach the bottom-right cell (n - 1, m - 1). The starting and ending cells may themselves be blocked. From any cell, the robot may move exactly one cell Right or one cell Down.
The robot has k override passes. Including one blocked cell in a path consumes exactly one pass, and the robot may consume at most k passes over its entire path.
Return the number of distinct paths from the start to the destination that use at most k override passes, modulo 10^9 + 7.
Function
numPaths(grid: int[][], k: int) → intExamples
Example 1
grid = [[1,0,1],[1,0,1],[1,1,1]]k = 1return = 4Four paths use at most one override pass. Three of them enter exactly one blocked cell, and the path that moves Down, Down, Right, Right enters no blocked cells.
Example 2
grid = [[1,0],[0,1]]k = 0return = 0Both possible first moves enter a blocked cell, but no override pass is available.
Constraints
1 <= n, m <= 2000 <= k <= 10- Every cell of
gridis either0or1.