Description
You are given a 2D integer array grid with size m x n. You are also given an integer k.
Your task is to calculate the number of paths you can take from the top-left cell (0, 0) to the bottom-right cell (m - 1, n - 1) satisfying the following constraints:
You can either move to the right or down. Formally, from the cell (i, j) you may move to the cell (i, j + 1) or to the cell (i + 1, j) if the target cell exists.
The XOR of all the numbers on the path must be equal to k.
Return the total number of such paths.
Since the answer can be very large, return the result modulo 109 + 7.
Example 1:
Input: grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11
Output: 3
Explanation:
The 3 paths are:
(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)
(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)
(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)
Example 2:
Input: grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2
Output: 5
Explanation:
The 5 paths are:
(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)
(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)
(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)
(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)
(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)
Example 3:
Input: grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10
Output: 0
Constraints:
1 <= m == grid.length <= 300
1 <= n == grid[r].length <= 300
0 <= grid[r][c] < 16
0 <= k < 16
Solution
BFS/DFS (MLE)
Use bfs to generate all the possible paths, and check if the final number is equal to k
.
Time complexity:
o
(
m
∗
n
)
o(m*n)
o(m∗n)
Space complexity:
o
(
m
∗
n
)
o(m*n)
o(m∗n)
DP
Solved after help… still don’t know why bfs would have memory limit error?
The transformation equation would be:
d
p
[
x
]
[
y
]
[
r
e
s
]
=
d
p
[
x
−
1
]
[
y
]
[
r
e
s
⊕
g
r
i
d
[
x
]
[
y
]
]
+
d
p
[
x
]
[
y
−
1
]
[
r
e
s
⊕
g
r
i
d
[
x
]
[
y
]
]
dp[x][y][res] = dp[x - 1][y][res \oplus grid[x][y]] + dp[x][y - 1][res \oplus grid[x][y]]
dp[x][y][res]=dp[x−1][y][res⊕grid[x][y]]+dp[x][y−1][res⊕grid[x][y]]
where
d
p
[
x
]
[
y
]
[
r
e
s
]
dp[x][y][res]
dp[x][y][res] denotes the possible paths at (x,y)
position, and the current XOR is res
. Since res = previous_res ^ grid[x][y]
, so previous_res = res ^ grid[x][y]
.
Because of the constrain, the 3rd dimension is 16
.
Time complexity:
o
(
m
∗
n
)
o(m*n)
o(m∗n)
Space complexity:
o
(
m
∗
n
)
o(m*n)
o(m∗n)
Code
BFS/DFS (MLE)
class Solution:
def countPathsWithXorValue(self, grid: List[List[int]], k: int) -> int:
queue = collections.deque([(0, 0, grid[0][0])])
res = 0
m, n = len(grid), len(grid[0])
while queue:
x, y, cur_res = queue.popleft()
if x == m - 1 and y == n - 1 and cur_res == k:
res += 1
res %= 1000000007
continue
if 0 <= x + 1 < m and 0 <= y < n:
queue.append((x + 1, y, cur_res ^ grid[x + 1][y]))
if 0 <= x < m and 0 <= y + 1 < n:
queue.append((x, y + 1, cur_res ^ grid[x][y + 1]))
return res
DP
class Solution:
def countPathsWithXorValue(self, grid: List[List[int]], k: int) -> int:
m, n = len(grid), len(grid[0])
dp = [[[-1] * 16 for _ in range(n)] for _ in range(m)]
for xor in range(16):
if xor == grid[0][0]:
dp[0][0][xor] = 1
for i in range(1, m):
for xor in range(16):
dp[i][0][xor] = dp[i - 1][0][xor ^ grid[i][0]]
for j in range(1, n):
for xor in range(16):
dp[0][j][xor] = dp[0][j - 1][xor ^ grid[0][j]]
for i in range(1, m):
for j in range(1, n):
for xor in range(16):
left = dp[i][j - 1][xor ^ grid[i][j]]
up = dp[i - 1][j][xor ^ grid[i][j]]
if left != -1 and up != -1:
dp[i][j][xor] = left + up
elif left != -1:
dp[i][j][xor] = left
elif up != -1:
dp[i][j][xor] = up
return max(0, dp[m - 1][n - 1][k]) % 1000000007