BoardPainting
SRM 577 · 2012-12-13 · by cgy4ever
Problem Statement
Fox Ciel paints in steps. In each step, she selects a group of white cells and paints all of them black. She is only allowed to select a contiguous sequence of white cells in a single row or in a single column.
Return the minimal number of steps needed to color the board.
Constraints
- n will be between 1 and 50, inclusive.
- m will be between 1 and 50, inclusive.
- target will contains exactly n elements.
- Each element in target will contains exactly m characters.
- Each character in target will be '#' or '.'.
{"#####"}
Returns: 1
Fox Ciel can select the entire row of the board and paint it all black in a single step.
{"#####",
".....",
"#####",
".....",
"#####"}
Returns: 3
In each turn, Ciel selects and colors one of the rows that should be black.
{"..#..",
"..#..",
"#####",
"..#..",
"..#.."}
Returns: 3
Note that each sequence of cells selected by Ciel has to be a contiguous sequence of white cells. It is not allowed to select a black cell. Therefore the best solution has three steps. One possibility: first she selects row 2, then the first two cells in column 2, and finally the last two cells in column 2.
{"#####",
"..#..",
"#####",
"..#..",
"#####"}
Returns: 5
{".#.#.",
"#####",
".#.#.",
"#####",
".#.#."}
Returns: 8
{"...................."}
Returns: 0
Note that the target can be totally white.
Submissions are judged against all 127 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BoardPainting with a public method int minimalSteps(vector<string> target) · 127 test cases · 2 s / 256 MB per case