Count Routes to Climb a Rectangular Grid Problem
Description
LeetCode Problem 3797.
You are given a string array grid of size n, where each string grid[i] has length m. The character grid[i][j] is one of the following symbols:
'.': The cell is available.'#': The cell is blocked.
You want to count the number of different routes to climb grid. Each route must start from any cell in the bottom row (row n - 1) and end in the top row (row 0).
However, there are some constraints on the route.
- You can only move from one available cell to another available cell.
- The Euclidean distance of each move is at most
d, wheredis an integer parameter given to you. The Euclidean distance between two cells(r1, c1),(r2, c2)issqrt((r1 - r2)^2 + (c1 - c2)^2). - Each move either stays on the same row or moves to the row directly above (from row
rtor - 1). - You cannot stay on the same row for two consecutive turns. If you stay on the same row in a move (and this move is not the last move), your next move must go to the row above.
Return an integer denoting the number of such routes. Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: grid = ["..","#."], d = 1
Output: 2
Explanation:
We label the cells we visit in the routes sequentially, starting from 1. The two routes are:
.2
#1
32
#1
We can move from the cell (1, 1) to the cell (0, 1) because the Euclidean distance is sqrt((1 - 0)^2 + (1 - 1)^2) = sqrt(1) <= d.
However, we cannot move from the cell (1, 1) to the cell (0, 0) because the Euclidean distance is sqrt((1 - 0)^2 + (1 - 0)^2) = sqrt(2) > d.
Example 2:
Input: grid = ["..","#."], d = 2
Output: 4
Explanation:
Two of the routes are given in example 1. The other two routes are:
2.
#1
23
#1
Note that we can move from (1, 1) to (0, 0) because the Euclidean distance is sqrt(2) <= d.
Example 3:
Input: grid = ["#"], d = 750
Output: 0
Explanation:
We cannot choose any cell as the starting cell. Therefore, there are no routes.
Example 4:
Input: grid = [".."], d = 1
Output: 4
Explanation:
The possible routes are:
.1
1.
12
21
Constraints:
1 <= n == grid.length <= 7501 <= m == grid[i].length <= 750grid[i][j]is'.'or'#'.1 <= d <= 750
Solution
Approach: Row-by-Row DP with Prefix Sums
Walk the grid from the bottom row to the top row and keep, for every available cell, how many routes end there. Because two sideways moves in a row are not allowed, we count two kinds of routes separately:
up[c]: routes that reached the cell by a move up, or that start there (bottom row, value1);side[c]: routes that reached the cell by a sideways move.
A route may end in the top row after either kind of move, so the answer is the sum of both over row 0, modulo 109 + 7.
Transitions for a row r:
up[c]is the sum ofup + sideof the cells in rowr + 1that are within distancedof(r, c). A move up changes the column bydcwith1 + dc^2 <= d^2, so the cells form a window[c - w, c + w]wherewis the largest integer with1 + w^2 <= d^2.side[c]is the sum ofupover the other cells of the same row withindcolumns, because a sideways move must follow a move up (or the start).
Blocked cells always hold 0. Each window sum is read from a prefix-sum array, so a row costs O(m).
Complexity: O(n * m) time and O(m) extra space.
class Solution:
def numberOfRoutes(self, grid: List[str], d: int) -> int:
mod = 10**9 + 7
n, m = len(grid), len(grid[0])
# w: how far left or right a move to the row above may reach
w = 0
while (w + 1) * (w + 1) + 1 <= d * d:
w += 1
below = [0] * m # ways to be at each cell of the row below, whatever the last move was
for r in range(n - 1, -1, -1):
row = grid[r]
up = [0] * m # ways to reach the cell by a move up (or by starting there)
if r == n - 1:
up = [1 if ch == '.' else 0 for ch in row]
else:
pre = [0] * (m + 1)
for c in range(m):
pre[c + 1] = pre[c] + below[c]
for c in range(m):
if row[c] == '.':
up[c] = (pre[min(m, c + w + 1)] - pre[max(0, c - w)]) % mod
pre = [0] * (m + 1)
for c in range(m):
pre[c + 1] = pre[c] + up[c]
cur = [0] * m
for c in range(m):
if row[c] == '.':
# a sideways move must follow a move up (or the start)
side = pre[min(m, c + d + 1)] - pre[max(0, c - d)] - up[c]
cur[c] = (up[c] + side) % mod
below = cur
return sum(below) % mod