#P4121. Chessboard Flip Connectivity
Chessboard Flip Connectivity
Chessboard Flip Connectivity
Jiajia has an ( n \times n ) chessboard where each cell has two faces: one white and one black. Initially, each cell shows one side up. The board is given as an ( n \times n ) grid of characters: 'B' represents a black side up and 'W' represents a white side up. Two cells of the same color are considered to be in the same connected component if they share a common edge.
Jiajia will perform a sequence of operations. In each operation, she flips a cell (i.e. changes its color from white to black or from black to white) at a given coordinate ( (x, y) ) and then wishes to determine the number of connected components for the black cells and the white cells respectively. Your task is to help compute these numbers after each flip.
inputFormat
The input consists of the following parts:
- An integer ( n ) denoting the size of the chessboard.
- ( n ) lines each containing a string of length ( n ). Each character is either 'B' or 'W', representing the initial color of the cell.
- An integer ( m ) denoting the number of flip operations.
- ( m ) lines, each containing two integers ( x ) and ( y ) (1-indexed), representing the coordinates of the cell to be flipped.
outputFormat
For each flip operation, output two integers separated by a space on a separate line. The first integer is the number of connected components of black cells, and the second integer is the number of connected components of white cells in the current state of the chessboard.
sample
3
BWB
WBW
BWB
2
1 1
2 2
4 3
3 1
</p>