Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Galaxies
- {
- class Program
- {
- static int[,] matrix;
- static bool[,] isVisited;
- static int counter = 0;
- static List<int> galaxiesCount = new List<int>();
- static void Main()
- {
- int[] nm = Console.ReadLine().Split().Select(int.Parse).ToArray();
- matrix = new int[nm[0], nm[1]];
- for (int i = 0; i < nm[0]; i++)
- {
- var line = Console.ReadLine().ToCharArray();
- for (int j = 0; j < nm[1]; j++)
- {
- matrix[i, j] = line[j] - '0';
- }
- }
- isVisited = new bool[nm[0], nm[1]];
- for (int row = 0; row < nm[0]; row++)
- {
- for (int col = 0; col < nm[1]; col++)
- {
- SizeCounter(matrix[row, col], row, col);
- if (counter != 0)
- {
- galaxiesCount.Add(counter);
- counter = 0;
- }
- }
- }
- galaxiesCount = galaxiesCount.OrderByDescending(x => x).ToList();
- for (int i = 0; i < galaxiesCount.Count; i++)
- {
- Console.WriteLine(galaxiesCount[i]);
- }
- }
- private static void SizeCounter(int number, int row, int col)
- {
- if (row < 0 || row >= matrix.GetLength(0) || col < 0 || col >= matrix.GetLength(1))
- {
- return;
- }
- if (isVisited[row, col] || number != matrix[row, col])
- {
- return;
- }
- isVisited[row, col] = true;
- if (number == 1)
- {
- counter++;
- }
- SizeCounter(matrix[row, col], row + 1, col);
- SizeCounter(matrix[row, col], row, col + 1);
- SizeCounter(matrix[row, col], row - 1, col);
- SizeCounter(matrix[row, col], row, col - 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement