#K46522. Largest Valid Subgrid Area
Largest Valid Subgrid Area
Largest Valid Subgrid Area
You are given a square grid of size \( N \times N \) consisting of characters 'R' and 'B'. For each grid (test case), your task is to find the area of the largest square subgrid which is valid. A square subgrid is said to be valid if either:
- All cells in the subgrid have the same color.
- The subgrid forms an alternating pattern. In an alternating pattern, the cell at position \( (i, j) \) (0-indexed) should be 'R' if \( i+j \) is even, and 'B' if \( i+j \) is odd. In other words, the subgrid should exactly match the \( 2 \times 2 \) chessboard pattern starting with an 'R' at the top-left corner.
Your goal is to output the area of the largest valid subgrid for each test case. The area of a subgrid of side length \( s \) is \( s^2 \).
Note: If there is no subgrid larger than \( 1 \times 1 \) that is valid, you must output 1 (since every single cell is trivially valid).
Mathematical Formulation:
Find the maximum \( s \) such that there exists an index \( (i, j) \) with \( 0 \leq i, j \leq N-s \) for which the subgrid \( G[i...i+s-1][j...j+s-1] \) satisfies one of the following conditions: \[ \text{AllSame}(G[i...i+s-1][j...j+s-1]) \quad \text{or} \quad \text{Alternating}(G[i...i+s-1][j...j+s-1]). \] Then, output \( s^2 \) for that test case.
inputFormat
The input is read from standard input (stdin) and has the following format:
T N row1 row2 ... rowN N row1 row2 ... rowN ...
Where:
- \( T \) is the number of test cases.
- For each test case, the first line contains an integer \( N \) denoting the size of the grid.
- The next \( N \) lines each contain a string of length \( N \) made up of characters 'R' and 'B'.
outputFormat
For each test case, output a single integer on a new line representing the area of the largest valid subgrid.
## sample3
3
RRR
RRR
RRR
3
RBR
BRB
RBR
3
RRB
BRB
RBR
9
9
4
</p>