2912. Number of Ways to Reach Destination in the Grid π
Description
You are given two integers n and m which represent the size of a 1-indexed grid. You are also given an integer k, a 1-indexed integer array source and a 1-indexed integer array dest, where source and dest are in the form [x, y] representing a cell on the given grid.
You can move through the grid in the following way:
- You can go from cell
[x1, y1]to cell[x2, y2]if eitherx1 == x2ory1 == y2. - Note that you can't move to the cell you are already in e.g.
x1 == x2andy1 == y2.
Return the number of ways you can reach dest from source by moving through the grid exactly k times.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: n = 3, m = 2, k = 2, source = [1,1], dest = [2,2] Output: 2 Explanation: There are 2 possible sequences of reaching [2,2] from [1,1]: - [1,1] -> [1,2] -> [2,2] - [1,1] -> [2,1] -> [2,2]
Example 2:
Input: n = 3, m = 4, k = 3, source = [1,2], dest = [2,3] Output: 9 Explanation: There are 9 possible sequences of reaching [2,3] from [1,2]: - [1,2] -> [1,1] -> [1,3] -> [2,3] - [1,2] -> [1,1] -> [2,1] -> [2,3] - [1,2] -> [1,3] -> [3,3] -> [2,3] - [1,2] -> [1,4] -> [1,3] -> [2,3] - [1,2] -> [1,4] -> [2,4] -> [2,3] - [1,2] -> [2,2] -> [2,1] -> [2,3] - [1,2] -> [2,2] -> [2,4] -> [2,3] - [1,2] -> [3,2] -> [2,2] -> [2,3] - [1,2] -> [3,2] -> [3,3] -> [2,3]
Constraints:
2 <= n, m <= 1091 <= k <= 105source.length == dest.length == 21 <= source[1], dest[1] <= n1 <= source[2], dest[2] <= m
Solutions
Solution 1: Dynamic Programming
Thinking
Each step changes the row or the column, and we want to sit at \(dest\) after \(k\) steps. The grid can be \(10^9\) on a side, so per-cell states are impossible. Relative to \(source\) there are only four kinds of cells: itself, same column, same row, or neither.
A four-vector \(f\) stores the number of ways to each kind; one step mixes only these four kinds, with coefficients from \(n\) and \(m\). After \(k\) iterations, pick the component that matches how \(dest\) sits relative to \(source\).
We define the following states:
- \(f[0]\) represents the number of ways to move from
sourcetosourceitself; - \(f[1]\) represents the number of ways to move from
sourceto another row in the same column; - \(f[2]\) represents the number of ways to move from
sourceto another column in the same row; - \(f[3]\) represents the number of ways to move from
sourceto another row and another column.
Initially, \(f[0] = 1\), and the other states are all \(0\).
For each state, we can calculate the current state based on the previous state, as follows:
We loop \(k\) times, and finally check whether source and dest are in the same row or column, and return the corresponding state.
The time complexity is \(O(k)\), where \(k\) is the number of moves. The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |