Downhill Spill
heights is a rectangle of integers giving the height of each cell. Water is poured onto the cell at row r, column c (both 0-based). From any wet cell the water flows onto each side neighbour (up, down, left or right) whose height is less than or equal to the height of that wet cell; the neighbour becomes wet and spreads the water in the same way. Water never flows uphill and never moves diagonally. Return the number of wet cells once the water stops, counting the starting cell.
For example, in [[3, 2, 1], [4, 5, 1], [1, 1, 1]] pouring onto row 0, column 0 wets (0, 0), then (0, 1) and (0, 2), then (1, 2), and finally the whole bottom row: 7 cells. The 4 and the 5 stay dry because they are higher than every wet cell next to them.
Example 1
The water runs along the top row, down the right column and across the bottom row; it cannot climb onto the 4 or the 5.
Example 2
Every side neighbour of the 1 is higher, so only the starting cell gets wet.
Constraints
1 <= rows, cols <= 80 <= heights[i][j] <= 90 <= r < rowsand0 <= c < cols
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.