Advertisement
Guest User

Pr07ConnectedAreasInAMatrix

a guest
Apr 7th, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. namespace Pr06ConnectedAreasInAMatrix
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     class Pr07ConnectedAreasInAMatrix
  7.     {
  8. //        private static readonly char[,] matrix =
  9. //        {
  10. //            {' ', ' ', ' ', '*', ' ', ' ', ' ', '*', ' '},
  11. //            {' ', ' ', ' ', '*', ' ', ' ', ' ', '*', ' '},
  12. //            {' ', ' ', ' ', '*', ' ', ' ', ' ', '*', ' '},
  13. //            {' ', ' ', ' ', ' ', '*', ' ', '*', ' ', ' '},
  14. //        };
  15.  
  16.         private static readonly char[,] matrix =
  17.         {
  18.             {'*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' '},
  19.             {'*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' '},
  20.             {'*', ' ', ' ', '*', '*', '*', '*', '*', ' ', ' '},
  21.             {'*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' '},
  22.             {'*', ' ', ' ', '*', ' ', ' ', ' ', '*', ' ', ' '},
  23.         };
  24.  
  25.         private static readonly int[] modR = {1, -1, 0, 0};
  26.         private static readonly int[] modC = {0, 0, 1, -1};
  27.         private static int areaSize = 0;
  28.  
  29.         private static void Main(string[] args)
  30.         {
  31.             char fill = 'a';
  32.             List<Area> areaList = new List<Area>();
  33.             for (int r = 0; r < matrix.GetLength(0); r++)
  34.             {
  35.                 for (int c = 0; c < matrix.GetLength(1); c++)
  36.                 {
  37.                     if (matrix[r, c] != ' ')
  38.                     {
  39.                         continue;
  40.                     }
  41.  
  42.                     FindAreas(r, c, fill);
  43.                     fill++;
  44.                     if (areaSize != 0)
  45.                     {
  46.                         areaList.Add(new Area(areaSize, r, c));
  47.                         areaSize = 0;
  48.                     }
  49.                 }
  50.             }
  51.  
  52.             Console.WriteLine("Total areas found: {0}", areaList.Count);
  53.             areaList.Sort();
  54.             for (int i = 0; i < areaList.Count; i++)
  55.             {
  56.                 Console.WriteLine("Area #{0} at ({1}, {2}), size: {3}",
  57.                     i + 1, areaList[i].Row, areaList[i].Col, areaList[i].Size);
  58.             }
  59.  
  60. //            PrintMatrix();
  61.         }
  62.  
  63.         private static void PrintMatrix()
  64.         {
  65.             for (int r = 0; r < matrix.GetLength(0); r++)
  66.             {
  67.                 for (int c = 0; c < matrix.GetLength(1); c++)
  68.                 {
  69.                     Console.Write("{0} ", matrix[r, c]);
  70.                 }
  71.                 Console.WriteLine();
  72.             }
  73.         }
  74.  
  75.         private static void FindAreas(int r, int c, char fill)
  76.         {
  77.             Stack<Tuple<int, int>> stack = new Stack<Tuple<int, int>>();
  78.             stack.Push(new Tuple<int, int>(r, c));
  79.             while (stack.Count > 0)
  80.             {
  81.                 int currRow = stack.Peek().Item1;
  82.                 int currCol = stack.Pop().Item2;
  83.                 if (currCol < 0 ||
  84.                     currRow < 0 ||
  85.                     currRow >= matrix.GetLength(0) ||
  86.                     currCol >= matrix.GetLength(1) ||
  87.                     matrix[currRow, currCol] != ' ')
  88.                 {
  89.                     continue;
  90.                 }
  91.  
  92.                 matrix[currRow, currCol] = fill;
  93.                 for (int i = 0; i < modR.Length; i++)
  94.                 {
  95.                     int row = currRow + modR[i];
  96.                     int col = currCol + modC[i];
  97.                     stack.Push(new Tuple<int, int>(row, col));
  98.                 }
  99.  
  100.                 areaSize++;
  101.             }
  102.         }
  103.     }
  104.  
  105.     internal class Area : IComparable<Area>
  106.     {
  107.         public Area(int size, int row, int col)
  108.         {
  109.             this.Size = size;
  110.             this.Row = row;
  111.             this.Col = col;
  112.         }
  113.  
  114.         public int Size { get; set; }
  115.  
  116.         public int Row { get; set; }
  117.  
  118.         public int Col { get; set; }
  119.  
  120.         public int CompareTo(Area other)
  121.         {
  122.             int result = other.Size.CompareTo(this.Size);
  123.             if (result == 0)
  124.             {
  125.                 result = this.Row.CompareTo(other.Row);
  126.                 if (result == 0)
  127.                 {
  128.                     result = this.Col.CompareTo(other.Col);
  129.                 }
  130.             }
  131.  
  132.             return result;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement