|
| 1 | +3393\. Count Paths With the Given XOR Value |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`. |
| 6 | + |
| 7 | +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**: |
| 8 | + |
| 9 | +* 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_. |
| 10 | +* The `XOR` of all the numbers on the path must be **equal** to `k`. |
| 11 | + |
| 12 | +Return the total number of such paths. |
| 13 | + |
| 14 | +Since the answer can be very large, return the result **modulo** <code>10<sup>9</sup> + 7</code>. |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +**Input:** grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11 |
| 19 | + |
| 20 | +**Output:** 3 |
| 21 | + |
| 22 | +**Explanation:** |
| 23 | + |
| 24 | +The 3 paths are: |
| 25 | + |
| 26 | +* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)` |
| 27 | +* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)` |
| 28 | +* `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)` |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +**Input:** grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2 |
| 33 | + |
| 34 | +**Output:** 5 |
| 35 | + |
| 36 | +**Explanation:** |
| 37 | + |
| 38 | +The 5 paths are: |
| 39 | + |
| 40 | +* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)` |
| 41 | +* `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)` |
| 42 | +* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)` |
| 43 | +* `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)` |
| 44 | +* `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)` |
| 45 | + |
| 46 | +**Example 3:** |
| 47 | + |
| 48 | +**Input:** grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10 |
| 49 | + |
| 50 | +**Output:** 0 |
| 51 | + |
| 52 | +**Constraints:** |
| 53 | + |
| 54 | +* `1 <= m == grid.length <= 300` |
| 55 | +* `1 <= n == grid[r].length <= 300` |
| 56 | +* `0 <= grid[r][c] < 16` |
| 57 | +* `0 <= k < 16` |
0 commit comments