|
| 1 | +<p>You are given a <strong>0-indexed</strong> matrix <code>grid</code> of order <code>n * n</code>. Each cell in this matrix has a value <code>grid[i][j]</code>, which is either a <strong>positive</strong> integer or <code>-1</code> representing a blocked cell.</p> |
| 2 | + |
| 3 | +<p>You can move from a non-blocked cell to any non-blocked cell that shares an edge.</p> |
| 4 | + |
| 5 | +<p>For any cell <code>(i, j)</code>, we represent its <strong>remoteness</strong> as <code>R[i][j]</code> which is defined as the following:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li>If the cell <code>(i, j)</code> is a <strong>non-blocked</strong> cell, <code>R[i][j]</code> is the sum of the values <code>grid[x][y]</code> such that there is <strong>no path</strong> from the <strong>non-blocked</strong> cell <code>(x, y)</code> to the cell <code>(i, j)</code>.</li> |
| 9 | + <li>For blocked cells, <code>R[i][j] == 0</code>.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>Return<em> the sum of </em><code>R[i][j]</code><em> over all cells.</em></p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2023/09/12/1-new.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 400px; height: 304px;" /></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | +<strong>Input:</strong> grid = [[-1,1,-1],[5,-1,4],[-1,3,-1]] |
| 21 | +<strong>Output:</strong> 39 |
| 22 | +<strong>Explanation:</strong> In the picture above, there are four grids. The top-left grid contains the initial values in the grid. Blocked cells are colored black, and other cells get their values as it is in the input. In the top-right grid, you can see the value of R[i][j] for all cells. So the answer would be the sum of them. That is: 0 + 12 + 0 + 8 + 0 + 9 + 0 + 10 + 0 = 39. |
| 23 | +Let's jump on the bottom-left grid in the above picture and calculate R[0][1] (the target cell is colored green). We should sum up the value of cells that can't be reached by the cell (0, 1). These cells are colored yellow in this grid. So R[0][1] = 5 + 4 + 3 = 12. |
| 24 | +Now let's jump on the bottom-right grid in the above picture and calculate R[1][2] (the target cell is colored green). We should sum up the value of cells that can't be reached by the cell (1, 2). These cells are colored yellow in this grid. So R[1][2] = 1 + 5 + 3 = 9. |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2023/09/12/2.png" style="width: 400px; height: 302px; background: #fff; border-radius: .5rem;" /></p> |
| 28 | + |
| 29 | +<p><strong class="example">Example 2:</strong></p> |
| 30 | + |
| 31 | +<pre> |
| 32 | +<strong>Input:</strong> grid = [[-1,3,4],[-1,-1,-1],[3,-1,-1]] |
| 33 | +<strong>Output:</strong> 13 |
| 34 | +<strong>Explanation:</strong> In the picture above, there are four grids. The top-left grid contains the initial values in the grid. Blocked cells are colored black, and other cells get their values as it is in the input. In the top-right grid, you can see the value of R[i][j] for all cells. So the answer would be the sum of them. That is: 3 + 3 + 0 + 0 + 0 + 0 + 7 + 0 + 0 = 13. |
| 35 | +Let's jump on the bottom-left grid in the above picture and calculate R[0][2] (the target cell is colored green). We should sum up the value of cells that can't be reached by the cell (0, 2). This cell is colored yellow in this grid. So R[0][2] = 3. |
| 36 | +Now let's jump on the bottom-right grid in the above picture and calculate R[2][0] (the target cell is colored green). We should sum up the value of cells that can't be reached by the cell (2, 0). These cells are colored yellow in this grid. So R[2][0] = 3 + 4 = 7. |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p><strong class="example">Example 3:</strong></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<strong>Input:</strong> grid = [[1]] |
| 43 | +<strong>Output:</strong> 0 |
| 44 | +<strong>Explanation:</strong> Since there are no other cells than (0, 0), R[0][0] is equal to 0. So the sum of R[i][j] over all cells would be 0. |
| 45 | +</pre> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | +<p><strong>Constraints:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><code>1 <= n <= 300</code></li> |
| 52 | + <li><code>1 <= grid[i][j] <= 10<sup>6</sup></code> or <code>grid[i][j] == -1</code></li> |
| 53 | +</ul> |
0 commit comments