using System; using System.Collections.Generic; using System.Text; namespace Softuniada { class Cell { public Cell(int row, int col) { this.Row = row; this.Col = col; } public int Row { get; set; } public int Col { get; set; } } class Asteroids { private static bool[,] visited; private static List board; private static SortedDictionary foundSizes; static void Main(string[] args) { var line = Console.ReadLine(); while (line != "end") { var matrixSizes = line.Split('x').Select(int.Parse).ToArray(); board = new List(); foundSizes = new SortedDictionary(); for (int i = 0; i < matrixSizes[0]; i++) { line = Console.ReadLine(); board.Add(line.ToCharArray()); } visited = new bool[board.Count, board[0].Length]; for (int i = 0; i < board.Count; i++) { for (int j = 0; j < board[0].Length; j++) { if (board[i][j] == '1' && !visited[i, j]) { visited[i, j] = true; Bfs(i, j); } else { visited[i, j] = true; } } } var count = 0; var keysReverse = foundSizes.Keys.Reverse().ToArray(); foreach (var key in keysReverse) { Console.WriteLine($"{foundSizes[key]}x{key}"); count += foundSizes[key]; } Console.WriteLine($"Total: {count}"); line = Console.ReadLine(); } } static void Bfs(int startRow, int startCol) { var queue = new Queue(); var startCell = new Cell(startRow, startCol); queue.Enqueue(startCell); int areaSize = 0; while (queue.Count > 0) { var currentCell = queue.Dequeue(); areaSize++; var rowModifications = new int[] { -1, 0, 0, 1 }; var colModifications = new int[] { 0, -1, 1, 0 }; for (int i = 0; i < rowModifications.Length; i++) { var newRow = currentCell.Row + rowModifications[i]; var newCol = currentCell.Col + colModifications[i]; if (IsInMatrix(newRow, newCol) && board[newRow][newCol] == '1' && !visited[newRow, newCol]) { var cell = new Cell(newRow, newCol); visited[newRow, newCol] = true; queue.Enqueue(cell); } } } if (!foundSizes.ContainsKey(areaSize)) { foundSizes.Add(areaSize, 0); } foundSizes[areaSize] = foundSizes[areaSize] + 1; } static bool IsInMatrix(int row, int col) { if (row >= 0 && row < visited.GetLength(0) && col >= 0 && col < visited.GetLength(1)) { return true; } return false; } } }