|
| 1 | +<p>You are given a <strong>0-indexed</strong> 2D matrix <code>grid</code> of size <code>m x n</code>, where <code>(r, c)</code> represents:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>A <strong>land</strong> cell if <code>grid[r][c] = 0</code>, or</li> |
| 5 | + <li>A <strong>water</strong> cell containing <code>grid[r][c]</code> fish, if <code>grid[r][c] > 0</code>.</li> |
| 6 | +</ul> |
| 7 | + |
| 8 | +<p>A fisher can start at any <strong>water</strong> cell <code>(r, c)</code> and can do the following operations any number of times:</p> |
| 9 | + |
| 10 | +<ul> |
| 11 | + <li>Catch all the fish at cell <code>(r, c)</code>, or</li> |
| 12 | + <li>Move to any adjacent <strong>water</strong> cell.</li> |
| 13 | +</ul> |
| 14 | + |
| 15 | +<p>Return <em>the <strong>maximum</strong> number of fish the fisher can catch if he chooses his starting cell optimally, or </em><code>0</code> if no water cell exists.</p> |
| 16 | + |
| 17 | +<p>An <strong>adjacent</strong> cell of the cell <code>(r, c)</code>, is one of the cells <code>(r, c + 1)</code>, <code>(r, c - 1)</code>, <code>(r + 1, c)</code> or <code>(r - 1, c)</code> if it exists.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | +<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example.png" style="width: 241px; height: 161px;" /> |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]] |
| 24 | +<strong>Output:</strong> 7 |
| 25 | +<strong>Explanation:</strong> The fisher can start at cell <code>(1,3)</code> and collect 3 fish, then move to cell <code>(2,3)</code> and collect 4 fish. |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong class="example">Example 2:</strong></p> |
| 29 | +<img alt="" src="https://assets.leetcode.com/uploads/2023/03/29/example2.png" /> |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]] |
| 32 | +<strong>Output:</strong> 1 |
| 33 | +<strong>Explanation:</strong> The fisher can start at cells (0,0) or (3,3) and collect a single fish. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>m == grid.length</code></li> |
| 41 | + <li><code>n == grid[i].length</code></li> |
| 42 | + <li><code>1 <= m, n <= 10</code></li> |
| 43 | + <li><code>0 <= grid[i][j] <= 10</code></li> |
| 44 | +</ul> |
0 commit comments