Count 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 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, where d is an integer parameter given to you. The Euclidean distance between two cells (r1, c1) and (r2, c2) is sqrt((r1 - r2)^2 + (c1 - c2)^2).
  • Each move either stays on the same row or moves to the row directly above, from row r to row r - 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
Inputgrid = ["..","#."], d = 1
Output2
The two valid routes start at cell (1, 1) and may move to (0, 1), but cannot move diagonally to (0, 0) because sqrt(2) > d.
Example 2
Inputgrid = ["..","#."], d = 2
Output4
The two routes from example 1 are valid, and two additional routes are possible because moving from (1, 1) to (0, 0) has distance sqrt(2) <= d.

Constraints

  • 1 <= n == grid.length <= 750
  • 1 <= m == grid[i].length <= 750
  • grid[i][j] is '.' or '#'.
  • 1 <= d <= 750

Asked at 1 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