Advertisement
Guest User

23

a guest
Jun 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01Garden
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int rows = int.Parse(Console.ReadLine());
  11.             char[][] array = new char[rows][];
  12.             int harmed = 0;
  13.             int lettuce = 0;
  14.             int carrot = 0;
  15.             int patoto = 0;
  16.             for (int i = 0; i < rows; i++)
  17.             {
  18.                 array[i] = Console.ReadLine()
  19.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  20.                     .Select(char.Parse)
  21.                     .ToArray();
  22.             }
  23.             string command = string.Empty;
  24.             while ((command = Console.ReadLine().ToLower()) != "end of harvest")
  25.             {
  26.                 var tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  27.                 string action = tokens[0];
  28.                 int row = int.Parse(tokens[1]);
  29.                 int col = int.Parse(tokens[2]);
  30.                 switch (action)
  31.                 {
  32.                     case "harvest":
  33.                         if (IsInside(array, row, col))
  34.                         {
  35.  
  36.  
  37.                             if (array[row][col] == 'L')
  38.                             {
  39.                                 array[row][col] = ' ';
  40.                                 lettuce++;
  41.  
  42.                             }
  43.                             else if (array[row][col] == 'P')
  44.                             {
  45.                                 array[row][col] = ' ';
  46.                                 patoto++;
  47.  
  48.                             }
  49.                             else if (array[row][col] == 'C')
  50.                             {
  51.                                 array[row][col] = ' ';
  52.                                 carrot++;
  53.  
  54.                             }
  55.                         }
  56.                         break;
  57.                     case "mole":
  58.                         string direction = tokens[3].ToLower();
  59.                         harmed += Annihilation(array, row, col, direction);
  60.                         break;
  61.                     default:
  62.                         break;
  63.                 }
  64.  
  65.             }
  66.             for (int i = 0; i < array.Length; i++)
  67.             {
  68.                 Console.Write(string.Join(" ", array[i]));
  69.                 Console.WriteLine();
  70.             }
  71.  
  72.             Console.WriteLine($"Carrots: {carrot}");
  73.             Console.WriteLine($"Potatoes: {patoto}");
  74.             Console.WriteLine($"Lettuce: {lettuce}");
  75.             Console.WriteLine($"Harmed vegetables: {harmed}");
  76.         }
  77.         public static bool IsInside(char[][] matrix, int row, int col)
  78.         {
  79.             return row >= 0 && col >= 0 && row < matrix.Length && col < matrix[row].Length;
  80.         }
  81.         public static int Annihilation(char[][] arr, int row, int col, string direction)
  82.         {
  83.             int counter = 0;
  84.             if (!IsInside(arr, row, col))
  85.             {
  86.                 return counter;
  87.             }
  88.             if (arr[row][col] != ' ')
  89.             {
  90.                 arr[row][col] = ' ';
  91.                 counter++;
  92.             }
  93.  
  94.             if (direction == "up")
  95.             {
  96.                 for (int i = row; i >= 0; i -= 2)
  97.                 {
  98.                     if (IsInside(arr, i, col) && arr[i][col] != ' ')
  99.                     {
  100.  
  101.                         arr[i][col] = ' ';
  102.                         counter++;
  103.  
  104.                     }
  105.  
  106.                 }
  107.             }
  108.             else if (direction == "down")
  109.             {
  110.                 for (int i = row; i < arr.Length; i += 2)
  111.                 {
  112.                     if (IsInside(arr, i, col) && arr[i][col] != ' ')
  113.                     {
  114.  
  115.                         arr[i][col] = ' ';
  116.                         counter++;
  117.  
  118.                     }
  119.  
  120.                 }
  121.             }
  122.             else if (direction == "right")
  123.             {
  124.                 for (int i = col; i < arr[row].Length; i += 2)
  125.                 {
  126.                     if (IsInside(arr, row, i) && arr[row][i] != ' ')
  127.                     {
  128.  
  129.                         arr[col][i] = ' ';
  130.                         counter++;
  131.  
  132.                     }
  133.  
  134.  
  135.                 }
  136.             }
  137.             else if (direction == "left")
  138.             {
  139.                 for (int i = col; i >= 0; i -= 2)
  140.                 {
  141.                     if (IsInside(arr, row, i) && arr[row][i] != ' ')
  142.                     {
  143.  
  144.                         arr[col][i] = ' ';
  145.                         counter++;
  146.  
  147.                     }
  148.  
  149.  
  150.                 }
  151.             }
  152.             return counter;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement