Dynamic Grid (Small)
InterviewTime limit5sMemory limit512 MB
Count the edge-connected groups of 1s in a binary grid after each point update by flood fill.
Problem
You have a grid with rows and columns, and every cell holds 0 or 1. You perform operations on the grid. Each operation is one of these two.
- Operation M: change one cell of the grid to 0 or 1.
- Operation Q: count the connected regions of 1s. A connected region of 1s is a maximal set of cells that all hold 1 such that you can walk from any cell of the set to any other cell of the set by moving only between cells that share an edge. Two cells that touch only at a corner are not connected.
Input
The first line holds the number of test cases . test cases follow.
Each test case starts with a line holding two integers and , the number of rows and the number of columns. The next lines each hold a string of characters that are 0 or 1, and these lines are the initial grid. Rows are numbered from 0 at the top, columns from 0 at the left.
The next line holds one integer , the number of operations. The next lines each hold one operation. An M operation is written M x y z and sets the cell in row and column to . A Q operation is written Q.
Output
For each test case, first print one line holding Case #x:, where is the test case number starting from 1. Then, for every Q operation of that test case, in order, print one line holding the number of connected regions of 1s. A test case with no Q operation prints only its Case #x: line.