This problem was used in the following GFU competitions:
GFU 2024 D2 Q9
GFU 2024 D1 Q10
You are given an n-by-n grid where each square is colored either black or white. A grid is correct if all of the following conditions are satisfied:
• Every row has the same number of black squares as it has white squares.
• Every column has the same number of black squares as it has white squares.
• No row or column has 3 or more consecutive squares of the same color.
Given a grid, determine whether it is correct.
The first input will be a single integer n that indicates the number of data sets that follow. Each data set will start with a single even integer s (2 <= s <= 24). The next s lines contain a string of length s consisting solely of the characters ‘B’ and ‘W’, representing the colors of the grid squares.
For each data set, if the grid is correct print the number 1 on a single line, otherwise print 0 on a single line.
Example Input:
4
4
WBBW
WBWB
BWWB
BWBW
4
BWWB
BWBB
WBBW
WBWW
6
BWBWWB
WBWBWB
WBBWBW
BBWBWW
BWWBBW
WWBWBB
6
WWBBWB
BBWWBW
WBWBWB
BWBWBW
BWBBWW
WBWWBB
Example Output:
1
0
0
1
Good Luck