Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Garden
- {
- public class Program
- {
- static void Main(string[] args)
- {
- //-------------------- Reading Input from the console----------------------------------------------------------------------
- int[] dimensions = Console.ReadLine().Split().Select(int.Parse).ToArray();
- //------------------- Set squareMatrix dimensions--------------------------------------------------------------------------
- int rows = dimensions[0];
- //------------------- Create squareMatrix with empty fields(value 0 means empty field)-------------------------------------
- int[,] squareMatrix = InitialFillMatrix(rows);
- //------------------ Fill the squareMatrix with given coordinates(value 1 means that there is a magic flower----------------
- FillInMatrixFlowers(squareMatrix);
- //------------------ Create a List with int array of the flowerIndexes-------------------------------------------------------
- List<int[]> currentFlowerIndex = GetFlowerIndexes(squareMatrix);
- //----------------- Bloom given flowers in all directions(up, down, left,right) until reaches the matrix boundaries----------
- SpreadFlowers(currentFlowerIndex, squareMatrix);
- //-----------------Print the matrix----------------------------------------------------------------------------------------
- PrintMatrix(squareMatrix);
- }
- public static void PrintMatrix(int[,] squareMatrix)
- {
- for (int row = 0; row < squareMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < squareMatrix.GetLength(1); col++)
- {
- Console.Write(squareMatrix[row,col] + " ");
- }
- Console.WriteLine();
- }
- }
- static void SpreadFlowers(List<int[]> currentFlowerIndexes, int[,] squareMatrix)
- {
- foreach (int[] flowerIndexes in currentFlowerIndexes)
- {
- int startFlowerRowIndex = flowerIndexes[0];
- int startFlowerColIndex = flowerIndexes[1];
- //---------------------- SpreadFlowers in increasing rows-------------------
- for (int row = startFlowerRowIndex; row < squareMatrix.GetLength(0); row++)
- {
- if (IsValidIndexes(row + 1, startFlowerColIndex, squareMatrix))
- {
- if (squareMatrix[row + 1, startFlowerColIndex] == 1)
- {
- squareMatrix[row + 1, startFlowerColIndex] += 1;
- }
- else
- {
- squareMatrix[row + 1, startFlowerColIndex] = 1;
- }
- }
- }
- //------------------ SpreadFlowers in decreasing rows----------------------
- for (int row = startFlowerRowIndex; row >= 0; row--)
- {
- if (IsValidIndexes(row - 1, startFlowerColIndex, squareMatrix))
- {
- if (squareMatrix[row - 1, startFlowerColIndex] == 1)
- {
- squareMatrix[row - 1, startFlowerColIndex] += 1;
- }
- else
- {
- squareMatrix[row - 1, startFlowerColIndex] = 1;
- }
- }
- }
- //----------------- SpreadFlowers in increasing cols-----------------------
- for (int col = startFlowerColIndex; col < squareMatrix.GetLength(1); col++)
- {
- if (IsValidIndexes(startFlowerRowIndex, col + 1, squareMatrix))
- {
- if (squareMatrix[startFlowerRowIndex, col + 1] == 1)
- {
- squareMatrix[startFlowerColIndex, col + 1] += 1;
- }
- else
- {
- squareMatrix[startFlowerRowIndex, col + 1] = 1;
- }
- }
- }
- //------------- SpreadFlowers in decreasing cols----------------------------
- for (int col = startFlowerColIndex; col >= 0; col--)
- {
- if (IsValidIndexes(startFlowerRowIndex, col - 1, squareMatrix))
- {
- if (squareMatrix[startFlowerRowIndex, col - 1] == 1)
- {
- squareMatrix[startFlowerColIndex, col - 1] += 1;
- }
- else
- {
- squareMatrix[startFlowerRowIndex, col - 1] = 1;
- }
- }
- }
- }
- }
- public static List<int[]> GetFlowerIndexes(int[,] squareMatrix)
- {
- List<int[]> flowers = new List<int[]>();
- for (int rowIndex = 0; rowIndex < squareMatrix.GetLength(0); rowIndex++)
- {
- for (int colIndex = 0; colIndex < squareMatrix.GetLength(1); colIndex++)
- {
- if (squareMatrix[rowIndex, colIndex] == 1)
- {
- flowers.Add(new [] {rowIndex, colIndex});
- }
- }
- }
- return flowers;
- }
- public static void FillInMatrixFlowers(int[,] squareMatrix)
- {
- string command = Console.ReadLine();
- while (command?.ToLower() != "bloom bloom plow")
- {
- int[] cmdArr = command.Split(" ").Select(int.Parse).ToArray();
- int rowIndex = cmdArr[0];
- int colIndex = cmdArr[1];
- if (IsValidIndexes(rowIndex, colIndex, squareMatrix))
- {
- squareMatrix[rowIndex, colIndex] = 1;
- }
- else
- {
- Console.WriteLine("Invalid coordinates.");
- command = Console.ReadLine();
- continue;
- }
- command = Console.ReadLine();
- }
- }
- public static int[,] InitialFillMatrix(int rows)
- {
- int[,] squareMatrix = new int[rows, rows];
- for (int rowIndex = 0; rowIndex < rows; rowIndex++)
- {
- for (int colIndex = 0; colIndex < rows; colIndex++)
- {
- squareMatrix[rowIndex, colIndex] = 0;
- }
- }
- return squareMatrix;
- }
- public static bool IsValidIndexes(int row, int col, int[,] squareMatrix)
- {
- return row >= 0 && row < squareMatrix.GetLength(0) &&
- col >= 0 && col < squareMatrix.GetLength(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment