Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _05.ConnectedAreasInAMatrix
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- /* Sample inputs
- * *
- * *
- * *
- * *
- END
- * * *
- * * *
- * *****
- * * *
- * * *
- END
- */
- internal class Area
- {
- public Area(int upLeftColumn, int upLeftRow)
- {
- this.Size = 0;
- this.UpLeftColumn = upLeftColumn;
- this.UpLeftRow = upLeftRow;
- }
- public int Size { get; private set; }
- public int UpLeftColumn { get; }
- public int UpLeftRow { get; }
- public void IncreaseSize()
- {
- this.Size++;
- }
- }
- internal static class Program
- {
- private static readonly List<Area> Areas = new List<Area>();
- private static readonly List<string> PassedColors = new List<string>();
- private static char emptyChar = ' ';
- private static char wallChar = '*';
- private static ConsoleColor currentColor;
- public static void Main()
- {
- string input = Console.ReadLine();
- List<char[]> matrix = new List<char[]>();
- while (input.ToUpper() != "END")
- {
- matrix.Add(input.ToCharArray());
- input = Console.ReadLine();
- }
- FindAllAreas(matrix.ToArray());
- }
- private static void FindAllAreas(char[][] matrix)
- {
- for (int row = 0; row < matrix.Length; row++)
- {
- for (int column = 0; column < matrix[0].Length; column++)
- {
- if (matrix[row][column] == emptyChar)
- {
- Areas.Add(new Area(column, row));
- currentColor = GetRandomColor();
- MeasureArea(matrix, row, column);
- }
- }
- }
- Console.SetCursorPosition(0, matrix.Length + 1);
- var results = Areas.OrderByDescending(a => a.Size).ThenBy(a => a.UpLeftRow).ThenBy(a => a.UpLeftColumn).ToArray();
- for (int i = 0; i < results.Length; i++)
- {
- Console.WriteLine(
- "Area #{0} at ({1}, {2}), size: {3}",
- i + 1,
- results[i].UpLeftRow,
- results[i].UpLeftColumn,
- results[i].Size);
- }
- }
- private static void MeasureArea(char[][] matrix, int currentRow, int currentColumn)
- {
- if (currentRow < 0 || currentRow >= matrix.Length || currentColumn < 0 || currentColumn >= matrix[0].Length)
- {
- return;
- }
- if (matrix[currentRow][currentColumn] != emptyChar)
- {
- return;
- }
- Areas.Last().IncreaseSize();
- matrix[currentRow][currentColumn] = 'x';
- Console.ForegroundColor = currentColor;
- Console.SetCursorPosition(currentColumn, currentRow);
- Console.Write(matrix[currentRow][currentColumn]);
- Console.ResetColor();
- MeasureArea(matrix, currentRow, currentColumn + 1); // right
- MeasureArea(matrix, currentRow + 1, currentColumn); // down
- MeasureArea(matrix, currentRow, currentColumn - 1); // left
- MeasureArea(matrix, currentRow - 1, currentColumn); // up
- }
- private static ConsoleColor GetRandomColor()
- {
- string[] colors = Enum.GetNames(typeof(ConsoleColor));
- string randomColor = colors[(new Random()).Next(colors.Length)];
- while (PassedColors.Contains(randomColor))
- {
- randomColor = colors[(new Random()).Next(colors.Length)];
- }
- PassedColors.Add(randomColor);
- return (ConsoleColor)Enum.Parse(typeof(ConsoleColor), randomColor);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement