Scribbling Witch
Time limit3sMemory limit512 MB
Fill the unknown cells of a grid so black cells form one tree and no two white cells share an edge, maximizing the black count.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation, Graph
- Solved
- No attempts yet
Problem
The witch ALBERTINE is drawing graffiti at the entrance of a rectangular barrier made of square cells, with columns and rows. In this world, graffiti is made by painting every cell either white or black.
Her familiar ANJA has already drawn part of the graffiti. The witch wants to paint all the remaining cells and complete it. She has one condition.
- Two different cells are called adjacent if they share an edge. In a finished graffiti, for any two black cells, there must be exactly one path from one to the other that follows adjacent black cells.
- No two white cells may be adjacent.
The witch likes black, so she wants a finished graffiti that meets the condition and has as many black cells as possible. What is the maximum number of black cells in a graffiti that satisfies her condition?
Input
H W
c11c12…c1W
c21c22…c2W
…
cH1cH2…cHW
is the height of the barrier and is its width.
is the state of the cell in row from the top and column from the left. If the cell is painted black, is '#'. If it is painted white, is '.'. If it is not yet painted, is '?'.
Output
Print the maximum possible number of black cells in one line, when the condition is satisfied.
If there is no way to paint the graffiti that satisfies the condition, print -1.
Constraints
- Each is one of
'#','.', or'?'.