Number of Ways to Reach Destination in the Grid Problem
Description
LeetCode Problem 2912.
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
[x_1, y_1]to cell[x_2, y_2]if eitherx_1 == x_2ory_1 == y_2. - Note that you can't move to the cell you are already in e.g.
x_1 == x_2andy_1 == y_2.
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 10^9 + 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 <= 10^91 <= k <= 10^5source.length == dest.length == 21 <= source[1], dest[1] <= n1 <= source[2], dest[2] <= m
Solution
Approach: Dynamic Programming on Four Kinds of Cells
Cells are equivalent when they relate to source in the same way, so we only track four kinds. f[t] is the number of ways to be at one fixed cell of kind t after the current number of moves:
f[0]: the source cell itself;f[1]: a cell in the same column assourcebut another row;f[2]: a cell in the same row but another column;f[3]: a cell in another row and another column.
At the start f[0] = 1 and the others are 0. One move gives, for a target cell of each kind, the sum over the cells that can reach it (same row or column, not itself):
g[0] = (n - 1) * f[1] + (m - 1) * f[2]g[1] = f[0] + (n - 2) * f[1] + (m - 1) * f[3]g[2] = f[0] + (m - 2) * f[2] + (n - 1) * f[3]g[3] = f[1] + f[2] + (n - 2) * f[3] + (m - 2) * f[3]
all taken modulo 10^9 + 7. Repeat k times, then look up the kind of dest relative to source: same row and column gives f[0], same column only gives f[1], same row only gives f[2], otherwise f[3].
Complexity: O(k) time, O(1) space.
class Solution:
def numberOfWays(
self, n: int, m: int, k: int, source: List[int], dest: List[int]
) -> int:
mod = 10**9 + 7
f = [1, 0, 0, 0]
for _ in range(k):
g = [0] * 4
g[0] = ((n - 1) * f[1] + (m - 1) * f[2]) % mod
g[1] = (f[0] + (n - 2) * f[1] + (m - 1) * f[3]) % mod
g[2] = (f[0] + (m - 2) * f[2] + (n - 1) * f[3]) % mod
g[3] = (f[1] + f[2] + (n - 2) * f[3] + (m - 2) * f[3]) % mod
f = g
if source[0] == dest[0]:
return f[0] if source[1] == dest[1] else f[2]
return f[1] if source[1] == dest[1] else f[3]