atcoder#ABC296H. [ABC296Ex] Unite
[ABC296Ex] Unite
Score : points
Problem Statement
We have a grid with rows and columns, where each square is painted black or white.
Here, at least one square is painted black.
The initial state of the grid is given as strings of length .
The square at the -th row from the top and -th column from the left is painted black if the -th character of is #
, and white if it is .
.
Takahashi wants to repaint some white squares (possibly zero) black so that the squares painted black are connected. Find the minimum number of squares he needs to repaint to achieve his objective.
Here, the squares painted black are said to be connected when, for every pair of squares painted black, there are a positive integer and a sequence of squares painted black such that , , and and share a side for every . It can be proved that, under the constraints of the problem, there is always a way for Takahashi to achieve his objective.
Constraints
- and are integers.
- is a string of length consisting of
#
and.
. - The given grid has at least one square painted black.
Input
The input is given from Standard Input in the following format:
Output
Print the minimum number of squares that Takahashi needs to repaint for the squares painted black to be connected.
3 5
...#.
.#...
....#
3
The initial grid looks as follows, where denotes the square at the -th row from the top and -th column from the left.
Assume that Takahashi repaints three squares (shown red in the figure below) black.
Then, we have the following squares painted black, including the ones painted black from the beginning. These squares are connected.
It is impossible to repaint two or fewer squares black so that the squares painted black are connected, so the answer is . Note that the squares painted white do not have to be connected.
3 3
###
###
###
0
All squares might be painted black from the beginning.
10 1
.
#
.
.
.
.
.
.
#
.
6