Routes to Climb a Rectangular Grid
You are given a string array grid of size n, where each string grid[i] has length m.
The character grid[i][j] is either '.' representing an available cell, or '#' representing a blocked cell.
Your goal is to count the total number of distinct routes that start from any available cell in the bottom row (row, n - 1) and end at any available cell in the top row (row, 0).
The route must follow these strict conditions:
- You can only traverse through available cells.
- The Euclidean distance ((row2 - row1)2 - (col2 - col1)2 )1/2 between the current cell and the next cell in a single move cannot exceed the integer d.
- Every move must either stay on the current row or advance to the row directly above it.
- You are strictly forbidden from making two horizontal moves in a row (staying on the same row for two consecutive turns). Return the number of such valid routes, modulo 109 + 7.
Example 1:
Input: grid = ["#.#", ".#."], d = 2
Output: 4
Explanation: The only available destination in the top row is at (0, 1). We can start from either available cell in the bottom row: (1, 0) or (1, 2). From (1, 0), we can jump diagonally directly to (0, 1) since the Euclidean distance of sqrt 2 is well within our limit of d=2. Alternatively, we can first make a horizontal jump from (1, 0) to (1, 2) (a distance of exactly 2), followed by a diagonal jump to (0, 1); this is perfectly legal because we only made one horizontal move before advancing rows. By applying the exact same logic starting from (1, 2), we can either jump directly to (0, 1), or jump horizontally to (1, 0) before making the final jump to (0, 1). This yields a total of 4 distinct valid routes.
Example 2:
Input: grid = ["###", "..."], d = 5
Output: 0
Explanation: In this scenario, the entire top row consists solely of blocked '#' cells. Since the problem strictly requires every valid route to end on an available cell in the top row, it is completely impossible to finish a route, regardless of our starting position or our generous distance limit. Therefore, there are 0 valid routes.
Now Your Turn!
Pick the correct output for the given inputInput: grid = [".", ".", "."], d = 1
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= n == grid.length <= 750
- 1 <= m == grid[i].length <= 750
- grid[i][j] is '.' or '#'.
- 1 <= d <= 750