Maximum Fish in a Grid
Problem statement
A rectangular integer matrix grid describes land and water: 0 is land, and each positive value is the fish count in a water cell.
Choose any water cell as a starting point. You may collect all fish in a visited cell and move repeatedly between water cells sharing an edge. A cell’s fish can be collected only once.
Return the largest total obtainable from one starting point, or 0 if every cell is land. Diagonal movement and movement through land are forbidden.
Function
findMaxFish(grid: int[][]) → intExamples
Example 1
grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]return = 7The component with3 and4 fish totals7, more than each other component.
Example 2
grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]return = 1The two positive cells are disconnected; either start collects1.
Example 3
grid = [[2,0,3],[4,5,0]]return = 11The connected cells2,4 and5 total11; the3-fish cell is isolated.
Constraints
1 <= grid.length <= 10.1 <= grid[r].length <= 10; all rows have equal length.0 <= grid[r][c] <= 10.