DrawingBlackCrosses
SRM 466 · 2009-11-12 · by Chmel_Tolstiy
SRM 466 · 2009-11-12 · by Chmel_Tolstiy · Dynamic Programming, String Manipulation
Problem Statement
Problem Statement
Pavel likes puzzles. One of his favorite puzzles is DrawingBlackCrosses. In this puzzle, the player starts with a rectangular grid of cells, where each cell is either white or black, and at most 8 of the cells are black. The player's goal is to achieve the state where all of the cells in the grid are black. To achieve this goal, a sequence of moves must be performed. In each move, a single white cell is selected and all white cells located in the same row and all white cells located in the same column as the selected cell, including the selected cell itself, are colored black. The moves are performed until there are no more white cells.
Each solution to this puzzle can be written as a sequence of cells, where the i-th cell in the sequence is the cell that was selected on the player's i-th move. Two solutions are considered to be different if these sequences have different lengths or if there's an index i such that the i-th cells in these sequences are different. You are given aString[] field representing the initial state of the grid. The j-th character in the i-th element of field is '.' if the cell in row i, column j of the grid is initially white and 'B' if this cell is initially black. Return the number of different solutions that exist for the given grid, modulo 1000000007.
Each solution to this puzzle can be written as a sequence of cells, where the i-th cell in the sequence is the cell that was selected on the player's i-th move. Two solutions are considered to be different if these sequences have different lengths or if there's an index i such that the i-th cells in these sequences are different. You are given a
Constraints
- field will contain between 1 and 20 elements, inclusive.
- Each element of field will contain between 1 and 20 characters, inclusive.
- All elements of field will have the same length.
- Each character in field will be either 'B' or '.'.
- field will contain no more than 8 'B' characters.
Examples
0)
{"."}
Returns: 1
Only one possible move.
1)
{"BBB",
"BBB"}
Returns: 1
No moves are necessary here since all the cells are already black.
2)
{"...",
"BB."}
Returns: 5
Let's number rows and columns of the grid as follows: 012 0 ... 1 BB. The following sequences of moves are possible (the first coordinate of each cell is its row number, the second coordinate is column number): 1. (0, 0), (1, 2); 2. (0, 1), (1, 2); 3. (0, 2); 4. (1, 2), (0, 0); 5. (1, 2), (0, 1).
3)
{"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."}
Returns: 563200757
4)
{"B..B",
"B.B.",
"...B",
"BB.B",
"...."}
Returns: 324
Submissions are judged against all 178 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class DrawingBlackCrosses with a public method int count(vector<string> field) · 178 test cases · 2 s / 256 MB per case