#P7532. [USACO21OPEN] Balanced Subsets P
[USACO21OPEN] Balanced Subsets P
Description
Farmer John's pasture can be viewed as a huge two-dimensional square grid made of square cells (imagine a huge chessboard). For each and , a cell can be represented by the ordered pair . Some cells contain grass.
A non-empty subset of cells is called "balanced" if the following conditions hold:
- All cells in the subset contain grass.
- The subset is 4-connected. In other words, from any cell in the subset to any other cell in the subset, there exists a path such that every pair of adjacent cells on the path are adjacent horizontally or vertically.
- If cells and () are in the subset, then all cells with are also in the subset.
- If cells and () are in the subset, then all cells with are also in the subset.
Compute the number of balanced subsets modulo .
Input Format
The first line contains .
The next lines each contain a string of length . If cell contains grass, then the -th character of the -th line is ; otherwise it is .
Output Format
Output the number of balanced subsets modulo .
2
GG
GG
13
4
GGGG
GGGG
GG.G
GGGG
642
Hint
Explanation for Sample 1
For this test case, every 4-connected subset is balanced.
G. .G .. .. GG .G .. G. GG .G G. GG GG
.., .., G., .G, .., .G, GG, G., G., GG, GG, .G, GG
Explanation for Sample 2
Below is an example of a subset that satisfies the second condition (4-connected) but does not satisfy the third condition:
GG..
.G..
GG..
....
Constraints
.
Translated by ChatGPT 5
京公网安备 11011102002149号