#C4319. Number of Islands II
Number of Islands II
Number of Islands II
Given an empty 2D grid of size \(m \times n\) where each cell is initially water (denoted by 0), you are asked to perform a sequence of operations. Each operation converts a water cell into land (denoted by 1). Two land cells are considered connected if they are adjacent horizontally or vertically. An island is defined as a maximal set of connected lands.
After each operation, you are required to output the current number of islands in the grid.
Note: If an operation is attempted on a cell that is already land, the number of islands remains unchanged.
Example:
Input: 3 3 5 0 0 0 1 1 2 2 1 1 1</p>Output: 1 1 2 3 1
inputFormat
The input is given via standard input and has the following format:
- The first line contains three integers: \(m\) \(n\) \(k\) where \(m\) and \(n\) are the grid dimensions and \(k\) is the number of operations.
- The following \(k\) lines each contain two integers \(x\) and \(y\), representing the row and column indices to convert to land (0-indexed).
outputFormat
Print a single line with \(k\) integers separated by a space. Each integer is the number of islands after the corresponding operation.
## sample3 3 5
0 0
0 1
1 2
2 1
1 1
1 1 2 3 1