Problem Statement
A platypus has been given the mission to paint the cells on a grid either black or white according to the following two conditions:
The platypus may have already painted some of the cells. The current state of the grid is given as a String[] grid. The i-th character of the j-th element of grid that represents the cell at row j, column i is 'W' if it has been painted white, 'B' if it has been painted black, and '?' if it does not have a color yet. Let X be the number of different ways how to color the rest of the grid according to the above conditions. Return the value X modulo 1000000007 (10^9 + 7). Two ways to color a grid are different if the color of at least one cell differs.
- For each color, all cells of that color must be connected. Formally, a pair of cells of color X is connected if there is a path of adjacent cells of color X between them. (Two cells are adjacent if they share a common edge.) We require that for each color, each pair of cells of that color must be connected.
- All the cells of each color must form a convex shape. A group of cells of a given color is convex if in each row and each column the cells of that color form a connected segment (possibly taking the whole row or column). In other words, whenever two cells of the same color share the same row or the same column, all cells between them must also have that particular color.
The platypus may have already painted some of the cells. The current state of the grid is given as a String[] grid. The i-th character of the j-th element of grid that represents the cell at row j, column i is 'W' if it has been painted white, 'B' if it has been painted black, and '?' if it does not have a color yet. Let X be the number of different ways how to color the rest of the grid according to the above conditions. Return the value X modulo 1000000007 (10^9 + 7). Two ways to color a grid are different if the color of at least one cell differs.
Definition
- Class:
- TwoConvexShapes
- Method:
- countWays
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int countWays(String[] grid)
- (be sure your method is public)
Constraints
- grid will contain between 1 and 50 elements, inclusive.
- Element 0 of grid will contain between 1 and 50 characters, inclusive.
- The remaining elements of grid will contain the same number of characters as element 0.
- Each character in each element of grid will be one of 'B', 'W', and '?' (quotes for clarity).
Examples
{"??", "??"}Returns: 14
Of all the 16 different ways to color the grid, only the following 2 are not valid. BW WB WB BW This is because cells of the same color are not connected.{"B?", "??"}Returns: 7
The following seven ways to color the grid are correct: BB BW BB BW BB BB BW BB BW WW WW WB BW BB{"WWB", "WWW", "WWW", "WWW"}Returns: 1
All colors have already been picked. The only possible coloring is already valid.{"BBBBBB", "WWBBBB", "WBBBBB"}Returns: 0
This coloring of the grid is not valid, the black cells do not form a convex shape.{"?BB?", "BBBB", "?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to color the remaining cells in the grid is valid.