AreaSplit
TCO05 Semi 2 · 2005-08-16 · by gepa
Problem Statement
Some square tiles have been selected from a N x M chessboard, building a pattern (not necessarily connected). You have the task of coloring some of the selected tiles red and all of the others blue so that the pattern composed by the blue tiles is a shifted copy of the pattern composed by the red tiles. One example is shown in the figure below (the white tiles in the left image are the selected tiles, which are colored in the right image).
You will be given a
Notes
- Since the red and blue patterns are translated copies of the same pattern, the returned value would be the same if we asked for the blue pattern (since we crop the returned value to the width/height of the resulting pattern).
- All elements of the returned String[] shall have the same length (the width of the red pattern).
Constraints
- board will contain between 1 and 50 elements, inclusive.
- Each element of board will have between 1 and 50 characters, inclusive.
- Each character of each element of board will be either '#' or '.'.
- There will be at least one '#' character in at least one element of board.
{".........",
".##......",
"..#.##...",
".#####...",
".#######.",
".#..###..",
"....#....",
"........."}
Returns: {"##..", ".#..", "####", "###.", "#..." }
The example from the problem statement.
{"##",
"##"}
Returns: {"#", "#" }
Here, two different half-patterns are possible: the horizontal {"##"} (width 2, height 1) and the vertical {"#", "#"} (width 1, height 2). We return the one with the smallest width.
{".#..#",
"##.##"}
Returns: {".#", "##" }
Note that the pattern need not be connected.
{".#.##",
"####.",
"#...."}
Returns: {"#.##", "#..." }
Even if the pattern is connected, the solution may not be connected.
{".###.",
"##.#.",
"..###"}
Returns: { }
We can not split this pattern into two equal patterns.
{".#.",
"#.#",
".#."}
Returns: {"#.", ".#" }
Here, both {"#.", ".#"} and {".#", "#."} are possible solutions. We have to return the first of these solutions (if we concatenate the two Strings of that solution we get "#..#" which comes lexicographically before ".##.", the concatenation of the Strings of the second solution).
Submissions are judged against all 87 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class AreaSplit with a public method vector<string> halfPattern(vector<string> board) · 87 test cases · 2 s / 256 MB per case