kaloyanTry

Garden Problem 2

Feb 13th, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Garden
  5. {
  6.     class Garden
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] inputDimension = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             int n = inputDimension[0];
  12.             int m = inputDimension[1];
  13.  
  14.             int[,] matrix = new int[n, m];
  15.             int flowerRow = 0;
  16.             int flowerCol = 0;
  17.  
  18.             string input = Console.ReadLine();
  19.             while (input != "Bloom Bloom Plow")
  20.             {
  21.  
  22.                 int[] inputPossition = input.Split().Select(int.Parse).ToArray();
  23.                 int currentRow = inputPossition[0];
  24.                 int currentCol = inputPossition[1];
  25.  
  26.                 if (!IsValidPosition(currentRow, currentCol, n, m))
  27.                 {
  28.                     Console.WriteLine("Invalid coordinates.");
  29.                 }
  30.  
  31.                 flowerRow = currentRow;
  32.                 flowerCol = currentCol;
  33.                
  34.  
  35.                 for (int r1 = 0; r1 < matrix.GetLength(0); r1++)
  36.                 {
  37.                     for (int c1 = 0; c1 < matrix.GetLength(1); c1++)
  38.                     {
  39.                         if (r1 == flowerRow || c1 == flowerCol)
  40.                         {
  41.                             matrix[r1, c1] += 1;
  42.                         }
  43.  
  44.                     }
  45.                 }
  46.  
  47.                 input = Console.ReadLine();
  48.             }
  49.  
  50.             for (int row = 0; row < matrix.GetLength(0); row++)
  51.             {
  52.                 for (int col = 0; col < matrix.GetLength(1); col++)
  53.                 {
  54.                     Console.Write(matrix[row, col] + " ");
  55.                    
  56.                 }
  57.                 Console.WriteLine();
  58.             }
  59.         }
  60.  
  61.         public static bool IsValidPosition(int row, int col, int rows, int cols)
  62.         {
  63.             if (row < 0 || row >= rows)
  64.             {
  65.                 return false;
  66.             }
  67.             if (col < 0 || col >= cols)
  68.             {
  69.                 return false;
  70.             }
  71.  
  72.             return true;
  73.         }
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment