gabi11

Algorithms - 06. Connected Areas in Matrix

Sep 8th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Algorithms
  6. {
  7.     class Program
  8.     {
  9.         static char wall = '*';
  10.         static char visited = 'v';
  11.         static List<Area> areas = new List<Area>();
  12.         static void Main(string[] args)
  13.         {
  14.             int rows = int.Parse(Console.ReadLine());
  15.             int cols = int.Parse(Console.ReadLine());
  16.             char[,] matrix = ReadMatrix(rows, cols);
  17.  
  18.             for (int row = 0; row < matrix.GetLength(0); row++)
  19.             {
  20.                 for (int col = 0; col < matrix.GetLength(1); col++)
  21.                 {
  22.                     FindConnectedArea(matrix, row, col);
  23.                 }
  24.             }
  25.  
  26.  
  27.             Console.WriteLine($"Total areas found: {areas.Count}");
  28.  
  29.             int positionCount = 1;
  30.  
  31.             foreach (var area in areas.OrderByDescending(a => a.size))
  32.             {
  33.                 Console.WriteLine($"Area #{positionCount++} at ({area.row}, {area.col}), size: {area.size}");
  34.             }
  35.  
  36.         }
  37.  
  38.         private static void FindConnectedArea(char[,] matrix, int row, int col)
  39.         {
  40.             if (matrix[row, col] == wall || matrix[row, col] == visited)
  41.             {
  42.                 return;
  43.             }
  44.  
  45.             Area area = new Area(row, col);
  46.             FillArea(matrix, row, col, area);
  47.  
  48.             areas.Add(area);
  49.         }
  50.  
  51.         private static void FillArea(char[,] matrix, int row, int col, Area area)
  52.         {
  53.             if (!IsInRange(matrix, row, col)
  54.                 || matrix[row, col] == visited
  55.                 || matrix[row, col] == wall)
  56.             {
  57.                 return;
  58.             }
  59.  
  60.             matrix[row, col] = visited;
  61.             area.size++;
  62.  
  63.             FillArea(matrix, row + 1, col, area);
  64.             FillArea(matrix, row, col + 1, area);
  65.             FillArea(matrix, row - 1, col, area);
  66.             FillArea(matrix, row, col - 1, area);
  67.  
  68.         }
  69.  
  70.         private static bool IsInRange(char[,] matrix, int row, int col)
  71.         {
  72.             return row >= 0 && row < matrix.GetLength(0) && col >= 0 && col < matrix.GetLength(1);
  73.         }
  74.  
  75.         private static char[,] ReadMatrix(int rows, int cols)
  76.         {
  77.             var matrix = new char[rows, cols];
  78.  
  79.             for (int i = 0; i < rows; i++)
  80.             {
  81.                 var currentRow = Console.ReadLine().ToCharArray();
  82.  
  83.                 for (int j = 0; j < currentRow.Length; j++)
  84.                 {
  85.                     matrix[i, j] = currentRow[j];
  86.                 }
  87.             }
  88.             return matrix;
  89.         }
  90.  
  91.         class Area : IComparable<Area>
  92.         {
  93.             public int row;
  94.             public int col;
  95.  
  96.             public int size;
  97.  
  98.             public Area(int row, int col)
  99.             {
  100.                 this.row = row;
  101.                 this.col = col;
  102.  
  103.                 this.size = 0;
  104.             }
  105.  
  106.             public int CompareTo(Area other)
  107.             {
  108.                 int comperator = other.size.CompareTo(this.size);
  109.  
  110.                 if (comperator == 0)
  111.                 {
  112.                     comperator = this.row.CompareTo(other.row);
  113.                 }
  114.  
  115.                 if (comperator == 0)
  116.                 {
  117.                     comperator = this.col.CompareTo(other.col);
  118.                 }
  119.  
  120.                 return comperator;
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment