Advertisement
Guest User

ConnectedAreasInAMatrix

a guest
Sep 27th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1. namespace _05.ConnectedAreasInAMatrix
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7. /* Sample inputs
  8.    *   *
  9.    *   *
  10.    *   *
  11.     * *  
  12. END
  13.  
  14. *  *   *  
  15. *  *   *  
  16. *  *****  
  17. *  *   *  
  18. *  *   *  
  19. END
  20. */
  21.     internal class Area
  22.     {
  23.         public Area(int upLeftColumn, int upLeftRow)
  24.         {
  25.             this.Size = 0;
  26.             this.UpLeftColumn = upLeftColumn;
  27.             this.UpLeftRow = upLeftRow;
  28.         }
  29.  
  30.         public int Size { get; private set; }
  31.  
  32.         public int UpLeftColumn { get; }
  33.  
  34.         public int UpLeftRow { get; }
  35.  
  36.  
  37.         public void IncreaseSize()
  38.         {
  39.             this.Size++;
  40.         }
  41.     }
  42.  
  43.     internal static class Program
  44.     {
  45.         private static readonly List<Area> Areas = new List<Area>();
  46.  
  47.         private static readonly List<string> PassedColors = new List<string>();
  48.  
  49.         private static char emptyChar = ' ';
  50.  
  51.         private static char wallChar = '*';
  52.  
  53.         private static ConsoleColor currentColor;
  54.  
  55.         public static void Main()
  56.         {
  57.             string input = Console.ReadLine();
  58.             List<char[]> matrix = new List<char[]>();
  59.  
  60.             while (input.ToUpper() != "END")
  61.             {
  62.                 matrix.Add(input.ToCharArray());
  63.  
  64.                 input = Console.ReadLine();
  65.             }
  66.  
  67.             FindAllAreas(matrix.ToArray());
  68.         }
  69.  
  70.         private static void FindAllAreas(char[][] matrix)
  71.         {
  72.             for (int row = 0; row < matrix.Length; row++)
  73.             {
  74.                 for (int column = 0; column < matrix[0].Length; column++)
  75.                 {
  76.                     if (matrix[row][column] == emptyChar)
  77.                     {
  78.                         Areas.Add(new Area(column, row));
  79.                         currentColor = GetRandomColor();
  80.                         MeasureArea(matrix, row, column);
  81.                     }
  82.                 }
  83.             }
  84.  
  85.             Console.SetCursorPosition(0, matrix.Length + 1);
  86.  
  87.             var results = Areas.OrderByDescending(a => a.Size).ThenBy(a => a.UpLeftRow).ThenBy(a => a.UpLeftColumn).ToArray();
  88.             for (int i = 0; i < results.Length; i++)
  89.             {
  90.                 Console.WriteLine(
  91.                     "Area #{0} at ({1}, {2}), size: {3}",
  92.                     i + 1,
  93.                     results[i].UpLeftRow,
  94.                     results[i].UpLeftColumn,
  95.                     results[i].Size);
  96.             }
  97.         }
  98.  
  99.         private static void MeasureArea(char[][] matrix, int currentRow, int currentColumn)
  100.         {
  101.             if (currentRow < 0 || currentRow >= matrix.Length || currentColumn < 0 || currentColumn >= matrix[0].Length)
  102.             {
  103.                 return;
  104.             }
  105.  
  106.             if (matrix[currentRow][currentColumn] != emptyChar)
  107.             {
  108.                 return;
  109.             }
  110.  
  111.             Areas.Last().IncreaseSize();
  112.             matrix[currentRow][currentColumn] = 'x';
  113.  
  114.             Console.ForegroundColor = currentColor;
  115.             Console.SetCursorPosition(currentColumn, currentRow);
  116.             Console.Write(matrix[currentRow][currentColumn]);
  117.             Console.ResetColor();
  118.  
  119.             MeasureArea(matrix, currentRow, currentColumn + 1); // right
  120.             MeasureArea(matrix, currentRow + 1, currentColumn); // down
  121.             MeasureArea(matrix, currentRow, currentColumn - 1); // left
  122.             MeasureArea(matrix, currentRow - 1, currentColumn); // up
  123.         }
  124.  
  125.         private static ConsoleColor GetRandomColor()
  126.         {
  127.             string[] colors = Enum.GetNames(typeof(ConsoleColor));
  128.             string randomColor = colors[(new Random()).Next(colors.Length)];
  129.             while (PassedColors.Contains(randomColor))
  130.             {
  131.                 randomColor = colors[(new Random()).Next(colors.Length)];
  132.             }
  133.  
  134.             PassedColors.Add(randomColor);
  135.  
  136.             return (ConsoleColor)Enum.Parse(typeof(ConsoleColor), randomColor);
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement