Advertisement
Guest User

Untitled

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