yanchevilian

02. Garden/ C# Advanced Exam - 25 October 2020

Sep 20th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Garden
  6. {
  7.     public class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //-------------------- Reading Input from the console----------------------------------------------------------------------
  12.  
  13.             int[] dimensions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.             //------------------- Set squareMatrix dimensions--------------------------------------------------------------------------
  15.  
  16.             int rows = dimensions[0];
  17.             //------------------- Create squareMatrix with empty fields(value 0 means empty field)-------------------------------------
  18.  
  19.             int[,] squareMatrix = InitialFillMatrix(rows);
  20.            
  21.             //------------------ Fill the squareMatrix with given coordinates(value 1 means that there is a magic flower----------------
  22.  
  23.             FillInMatrixFlowers(squareMatrix);
  24.  
  25.             //------------------ Create a List with int array of the flowerIndexes-------------------------------------------------------
  26.             List<int[]> currentFlowerIndex = GetFlowerIndexes(squareMatrix);
  27.  
  28.             //----------------- Bloom given flowers in all directions(up, down, left,right) until reaches the matrix boundaries----------
  29.             SpreadFlowers(currentFlowerIndex, squareMatrix);
  30.  
  31.             //-----------------Print the matrix----------------------------------------------------------------------------------------
  32.             PrintMatrix(squareMatrix);
  33.  
  34.         }
  35.  
  36.         public static void PrintMatrix(int[,] squareMatrix)
  37.         {
  38.             for (int row = 0; row < squareMatrix.GetLength(0); row++)
  39.             {
  40.                 for (int col = 0; col < squareMatrix.GetLength(1); col++)
  41.                 {
  42.                     Console.Write(squareMatrix[row,col] + " ");
  43.                 }
  44.                 Console.WriteLine();
  45.             }
  46.         }
  47.  
  48.         static void SpreadFlowers(List<int[]> currentFlowerIndexes, int[,] squareMatrix)
  49.         {
  50.             foreach (int[] flowerIndexes in currentFlowerIndexes)
  51.             {
  52.                 int startFlowerRowIndex = flowerIndexes[0];
  53.                 int startFlowerColIndex = flowerIndexes[1];
  54.                 //---------------------- SpreadFlowers in increasing rows-------------------
  55.                 for (int row = startFlowerRowIndex; row < squareMatrix.GetLength(0); row++)
  56.                 {
  57.                     if (IsValidIndexes(row + 1, startFlowerColIndex, squareMatrix))
  58.                     {
  59.                         if (squareMatrix[row + 1, startFlowerColIndex] == 1)
  60.                         {
  61.                             squareMatrix[row + 1, startFlowerColIndex] += 1;
  62.                         }
  63.                         else
  64.                         {
  65.                             squareMatrix[row + 1, startFlowerColIndex] = 1;
  66.                         }
  67.                     }
  68.                 }
  69.                 //------------------ SpreadFlowers in decreasing rows----------------------
  70.                 for (int row = startFlowerRowIndex; row >= 0; row--)
  71.                 {
  72.                     if (IsValidIndexes(row - 1, startFlowerColIndex, squareMatrix))
  73.                     {
  74.                         if (squareMatrix[row - 1, startFlowerColIndex] == 1)
  75.                         {
  76.                             squareMatrix[row - 1, startFlowerColIndex] += 1;
  77.                         }
  78.                         else
  79.                         {
  80.                             squareMatrix[row - 1, startFlowerColIndex] = 1;
  81.                         }
  82.                     }
  83.                 }
  84.                 //----------------- SpreadFlowers in increasing cols-----------------------
  85.                 for (int col = startFlowerColIndex; col < squareMatrix.GetLength(1); col++)
  86.                 {
  87.                     if (IsValidIndexes(startFlowerRowIndex, col + 1, squareMatrix))
  88.                     {
  89.                         if (squareMatrix[startFlowerRowIndex, col + 1] == 1)
  90.                         {
  91.                             squareMatrix[startFlowerColIndex, col + 1] += 1;
  92.                         }
  93.                         else
  94.                         {
  95.                             squareMatrix[startFlowerRowIndex, col + 1] = 1;
  96.                         }
  97.                     }
  98.                 }
  99.                 //------------- SpreadFlowers in decreasing cols----------------------------
  100.                 for (int col = startFlowerColIndex; col >= 0; col--)
  101.                 {
  102.                     if (IsValidIndexes(startFlowerRowIndex, col - 1, squareMatrix))
  103.                     {
  104.                         if (squareMatrix[startFlowerRowIndex, col - 1] == 1)
  105.                         {
  106.                             squareMatrix[startFlowerColIndex, col - 1] += 1;
  107.                         }
  108.                         else
  109.                         {
  110.                             squareMatrix[startFlowerRowIndex, col - 1] = 1;
  111.                         }
  112.  
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.         public static List<int[]> GetFlowerIndexes(int[,] squareMatrix)
  118.         {
  119.             List<int[]> flowers = new List<int[]>();
  120.             for (int rowIndex = 0; rowIndex < squareMatrix.GetLength(0); rowIndex++)
  121.             {
  122.                 for (int colIndex = 0; colIndex < squareMatrix.GetLength(1); colIndex++)
  123.                 {
  124.                     if (squareMatrix[rowIndex, colIndex] == 1)
  125.                     {
  126.                         flowers.Add(new [] {rowIndex, colIndex});
  127.                     }
  128.                 }
  129.             }
  130.  
  131.             return flowers;
  132.         }
  133.         public static void FillInMatrixFlowers(int[,] squareMatrix)
  134.         {
  135.             string command = Console.ReadLine();
  136.             while (command?.ToLower() != "bloom bloom plow")
  137.             {
  138.                 int[] cmdArr = command.Split(" ").Select(int.Parse).ToArray();
  139.                 int rowIndex = cmdArr[0];
  140.                 int colIndex = cmdArr[1];
  141.                 if (IsValidIndexes(rowIndex, colIndex, squareMatrix))
  142.                 {
  143.                     squareMatrix[rowIndex, colIndex] = 1;
  144.                 }
  145.                 else
  146.                 {
  147.                     Console.WriteLine("Invalid coordinates.");
  148.                     command = Console.ReadLine();
  149.                     continue;
  150.                 }
  151.  
  152.                 command = Console.ReadLine();
  153.             }
  154.         }
  155.  
  156.         public static int[,] InitialFillMatrix(int rows)
  157.         {
  158.             int[,] squareMatrix = new int[rows, rows];
  159.  
  160.             for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  161.             {
  162.                 for (int colIndex = 0; colIndex < rows; colIndex++)
  163.                 {
  164.                     squareMatrix[rowIndex, colIndex] = 0;
  165.                 }
  166.             }
  167.  
  168.             return squareMatrix;
  169.         }
  170.  
  171.         public static bool IsValidIndexes(int row, int col, int[,] squareMatrix)
  172.         {
  173.             return row >= 0 && row < squareMatrix.GetLength(0) &&
  174.                    col >= 0 && col < squareMatrix.GetLength(1);
  175.         }
  176.     }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment