#P5198. Ice Cream Machine Optimization
Ice Cream Machine Optimization
Ice Cream Machine Optimization
Farmer John is starting his ice cream business and has built a machine that produces scoops of ice cream in the shape of irregular figures. The machine outputs an N × N grid (with 1 ≤ N ≤ 1000), where each character is either a '#' indicating a 1×1 unit of ice cream or a '.' indicating an empty space.
Unfortunately, the machine might produce several disconnected ice cream "balls". Two '#' cells are in the same ice cream ball if you can travel between them repeatedly moving north, south, east, or west through other '#' cells. Note that an ice cream ball may have holes inside it. In this case, the boundary of the hole is still counted towards the ball's perimeter. Also, balls contained entirely within another ball are considered separate.
Your task is to find the ball with the largest area (the count of '#' cells). If there are multiple balls with the same largest area, choose the one with the smallest perimeter. For example, in the grid below:
##.... ....#. .#..#. .##### ...### ....##
There are two ice cream balls. The small ball has an area of 2 and a perimeter of 6, while the large ball has an area of 13 and a perimeter of 22. In another case, consider the grid:
##### #...# #.#.# #...# #####
Here, a ball of area 1 is enclosed within a ball of area 16. You must compute the area and perimeter for the ball with the maximum area (and with minimum perimeter in case of ties).
This is important to Farmer John since he ultimately wants to minimize the ratio of perimeter to area for the ice cream, which he calls the "ice-cream ratio." A smaller ratio means that the ice cream melts slower because there is less surface area per unit mass.
inputFormat
The first line of input contains an integer N (1 ≤ N ≤ 1000). The next N lines each contain exactly N characters. Each character is either a '#' (representing a unit square of ice cream) or a '.' (representing an empty space).
outputFormat
Output two space-separated integers: the area (number of '#' cells) and the perimeter of the largest ice cream ball. If multiple balls share the largest area, output the one with the smallest perimeter.
sample
6
##....
....#.
.#..#.
.#####
...###
....##
13 22