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