#K60817. Taco Cluster Counter
Taco Cluster Counter
Taco Cluster Counter
You are given a square grid of size \(n \times n\) where each cell contains either a 0 (empty) or 1 (product). Two cells containing 1 are considered to be in the same cluster if they are adjacent horizontally or vertically.
Your task is to count the number of distinct clusters and report the sizes of each cluster in ascending order.
Note: Two cells are adjacent if they share a common side. Diagonal cells are not considered adjacent.
For example, given the grid below:
1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 1
The answer is 3 clusters with sizes 1, 4, and 4 respectively.
inputFormat
The first line contains an integer \(n\) representing the number of rows (and columns) in the grid. The next \(n\) lines each contain \(n\) space-separated integers (either 0 or 1) representing the grid.
For example:
5 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 1
outputFormat
Output two lines. The first line should contain the number of clusters in the grid.
The second line should list the sizes of each cluster in ascending order, separated by a space. If there are no clusters, output only 0.
For the sample input above, the output should be:
3 1 4 4## sample
5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 1 1
3
1 4 4
</p>