#K76572. Counting Coral Clusters
Counting Coral Clusters
Counting Coral Clusters
You are given a grid representing a section of an underwater coral reef. Each cell of the grid contains either a 'C' representing coral or a 'W' representing water.
A coral cluster is defined as a group of adjacent 'C' cells, where cells are considered adjacent if they are directly connected horizontally or vertically (diagonal connections do not count).
Your task is to count the number of distinct coral clusters in the grid.
The mathematical definition of connectivity can be summarized as follows:
\(\text{clusters} = \sum_{i=1}^{n}\sum_{j=1}^{m} \mathbf{1}_{\{\text{cell}(i,j)=C \text{ and not visited}\}}\)
where the connectivity is defined by the 4-neighbor adjacency rule.
inputFormat
The input is provided via stdin
in the following format:
- The first line contains two integers n and m (1 ≤ n, m ≤ 1000) representing the number of rows and columns of the grid respectively.
- The following n lines each contain m tokens separated by spaces. Each token is either 'C' (coral) or 'W' (water).
outputFormat
Output a single integer to stdout
— the number of distinct coral clusters in the grid.
1 1
C
1