Pineapple Farming
Time limit2sMemory limit256 MB
On a grid of heights with all outside cells treated as height 0, find the largest connected set of cells that forms a puddle bounded by strictly taller neighbors for some threshold h.
- Level
Hard8 of 10
- Topics
- Union-find, BFS, DFS, Simulation
- Solved
- No attempts yet
Problem
Pineapples are tropical fruit that need heavy rain to grow. Byeongchan bought an entire mountain in a rainy tropical area to put more pineapple on pizza.
He wants to plant pineapples on all of the land, but a problem came up. Very heavy rain can leave a huge puddle on the mountain, and pineapples under water cannot be grown. Byeongchan wants to find the largest puddle on this mountain.
A set of cells is a puddle when it satisfies all of the following conditions.
-
The mountain has columns and rows, and each cell has an integer height from to . Every height outside the mountain is .
-
The cells of one puddle are connected. Connected means each cell reaches every other cell by moves to the four neighboring cells in the up, down, left, and right directions, and diagonal contact does not count as connected.
-
There is an integer such that:
- Every cell of the puddle has height at most .
- Every cell outside the puddle and adjacent to the puddle in one of the four directions has height at least .
Input
The first line gives and , the number of rows and columns of the mountain. ()
Each of the next lines gives numbers.
The th number on line () is the height of the cell in row and column .
Output
Print the size of the largest puddle on the mountain, measured as the number of cells. If there is no puddle, print .
Hint
The shaded area in the following arrangement is the largest puddle.