#K80907. Counting Isolated Crop Clusters
Counting Isolated Crop Clusters
Counting Isolated Crop Clusters
You are given a grid of size \(m \times n\) where each cell may either contain a crop, denoted by the character 'C'
, or barren land (denoted by any other character, for example, 'B'
). Two crop cells are considered connected if they share a side (i.e., they are adjacent either horizontally or vertically). Diagonal neighbors are not considered connected.
Your task is to count the number of isolated crop clusters in the grid. A crop cluster is defined as a connected component consisting solely of cells with 'C'
characters.
Hint: You may use a depth-first search algorithm to traverse each connected component.
inputFormat
The input is given via standard input (stdin) in the following format:
- The first line contains two integers \(m\) and \(n\) (\(1 \le m, n \le 10^3\)) representing the number of rows and columns of the grid, respectively.
- The next \(m\) lines each contain a string of length \(n\) consisting of characters. The character
'C'
represents a crop and any other character (for example,'B'
) represents barren land.
outputFormat
Output a single integer to standard output (stdout) representing the number of isolated crop clusters in the grid.## sample
4 5
CCBCC
BCCCC
CBBCC
CCBCC
2
</p>