Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Galaxies
  6. {
  7.     class Program
  8.     {
  9.         static int[,] matrix;
  10.         static bool[,] isVisited;
  11.         static int counter = 0;
  12.         static List<int> galaxiesCount = new List<int>();
  13.  
  14.         static void Main()
  15.         {
  16.             int[] nm = Console.ReadLine().Split().Select(int.Parse).ToArray();
  17.             matrix = new int[nm[0], nm[1]];
  18.  
  19.             for (int i = 0; i < nm[0]; i++)
  20.             {
  21.                 var line = Console.ReadLine().ToCharArray();
  22.                 for (int j = 0; j < nm[1]; j++)
  23.                 {
  24.                     matrix[i, j] = line[j] - '0';
  25.                 }
  26.             }
  27.  
  28.             isVisited = new bool[nm[0], nm[1]];
  29.             for (int row = 0; row < nm[0]; row++)
  30.             {
  31.                 for (int col = 0; col < nm[1]; col++)
  32.                 {
  33.                     SizeCounter(matrix[row, col], row, col);
  34.                     if (counter != 0)
  35.                     {
  36.                         galaxiesCount.Add(counter);
  37.                         counter = 0;
  38.                     }
  39.                 }
  40.             }
  41.  
  42.             galaxiesCount = galaxiesCount.OrderByDescending(x => x).ToList();
  43.             for (int i = 0; i < galaxiesCount.Count; i++)
  44.             {
  45.                 Console.WriteLine(galaxiesCount[i]);                
  46.             }
  47.         }
  48.  
  49.         private static void SizeCounter(int number, int row, int col)
  50.         {
  51.             if (row < 0 || row >= matrix.GetLength(0) || col < 0 || col >= matrix.GetLength(1))
  52.             {
  53.                 return;
  54.             }
  55.             if (isVisited[row, col] || number != matrix[row, col])
  56.             {                
  57.                 return;
  58.             }
  59.  
  60.             isVisited[row, col] = true;
  61.             if (number == 1)
  62.             {
  63.                 counter++;
  64.             }
  65.             SizeCounter(matrix[row, col], row + 1, col);
  66.             SizeCounter(matrix[row, col], row, col + 1);
  67.             SizeCounter(matrix[row, col], row - 1, col);
  68.             SizeCounter(matrix[row, col], row, col - 1);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement