We have a grid with R rows and C columns in which every entry is either 0 or 1. We are going to perform N operations on the grid, each of which is one of the following:
- Operation M: Change a number in one cell of the grid to 0 or 1
- Operation Q: Determine the number of different connected regions of 1s. A connected region of 1s is a subset of cells that are all 1, in which any cell in the region can be reached from any other cell in the region by traveling between cells along edges (not corners).
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with one line with two integers, R and C, which represent the number of rows and columns in the grid. Then, there are R lines of C characters each, in which every character is either 0 or 1. These lines represent the initial state of the grid.
The next line has one integer, N, the number of operations to perform on the grid. N more lines follow; each has one operation. All operation Ms will be of the form M x y z, meaning that the cell at row x and column y should be changed to the value z. All operation Qs will be of the form Q.
For each test case, output one line containing “Case #x:”, where x is the test case number (starting from 1). Then, for every operation Q in the test case, in order, output one line containing the number of connected regions of 1s.
1 ≤ T ≤ 10.
1 ≤ N ≤ 10.
1 ≤ N ≤ 1000.
简单题，DFS 即可解决。从矩阵中为1且未访问过的坐标开始 DFS，直至所有点均已遍历过，从矩阵中为1的坐标调用 DFS 的次数即为题中所求。具体实现可以使用布尔型矩阵标记访问过的坐标或者原地修改数组值。这里使用原地修改的方式，代码量略小一点。
Google Code Jam 上都是自己下载输入文件，上传结果，这里我们使用输入输出重定向的方法解决这个问题。举个例子，将这段代码保存为
java Solution < A-large-practice.in > A-large-practice.out
这种方式处理各种不同 OJ 平台的输入输出较为方便。
对应每一个 Q，每个坐标点有四个方向，故单次求解时间复杂度 $$O(4RC)$$, 空间复杂度 $$O(RC)$$. 有 T 次测试数据及 N/2 次查询，故最终总的时间复杂度为 $$O(TNRC)$$, 代入 Large 数据集可知约为 $$O(10^8)$$, 几分钟内是可以解出来的。
- 《挑战程序设计竞赛》穷竭搜索一章，POJ 八连通变形题