Advertisement
krasizorbov

TheGarden

Jun 17th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.78 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace TheGarden
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int r = int.Parse(Console.ReadLine());
  11.             var matrix = new char[r][];
  12.             int carrotCount = 0;
  13.             int potatoCount = 0;
  14.             int lettuceCount = 0;
  15.             int harmedCount = 0;
  16.  
  17.             for (int i = 0; i < matrix.Length; i++)
  18.             {
  19.                 char[] input = Console.ReadLine()
  20.                     .Split(" ",StringSplitOptions.RemoveEmptyEntries)
  21.                     .Select(char.Parse).ToArray();
  22.  
  23.                 matrix[i] = new char[input.Length];
  24.                 for (int j = 0; j < matrix[i].Length; j++)
  25.                 {
  26.                     matrix[i][j] = input[j];
  27.                 }
  28.             }
  29.  
  30.             while (true)
  31.             {
  32.                 string[] arr = Console.ReadLine().Split().ToArray();
  33.  
  34.                 if (arr[0] == "Harvest")
  35.                 {
  36.                     int row = int.Parse(arr[1]);
  37.                     int col = int.Parse(arr[2]);
  38.                     bool isTrue = false;
  39.                     for (int i = 0; i < matrix.Length; i++)
  40.                     {
  41.                         for (int j = 0; j < matrix[i].Length; j++)
  42.                         {
  43.                             if (isInside(matrix, row, col))
  44.                             {
  45.                                 if (matrix[row][col] == 'L')
  46.                                 {
  47.                                     lettuceCount++;
  48.                                     matrix[row][col] = ' ';
  49.                                     isTrue = true;
  50.                                     break;
  51.                                 }
  52.                                 else if (matrix[row][col] == 'P')
  53.                                 {
  54.                                     potatoCount++;
  55.                                     matrix[row][col] = ' ';
  56.                                     isTrue = true;
  57.                                     break;
  58.                                 }
  59.                                 else if (matrix[row][col] == 'C')
  60.                                 {
  61.                                     carrotCount++;
  62.                                     matrix[row][col] = ' ';
  63.                                     isTrue = true;
  64.                                     break;
  65.                                 }
  66.                             }
  67.                             else
  68.                             {
  69.                                 isTrue = true;
  70.                                 break;
  71.                             }
  72.                         }
  73.                         if (isTrue == true)
  74.                         {
  75.                             break;
  76.                         }
  77.                     }
  78.                 }
  79.                 else if (arr[0] == "Mole")
  80.                 {
  81.                     int row = int.Parse(arr[1]);
  82.                     int col = int.Parse(arr[2]);
  83.                     string direction = arr[3];
  84.  
  85.                     if (direction == "up")
  86.                     {
  87.                         if (isInside(matrix, row, col))
  88.                         {
  89.                             Check(matrix, row, col);
  90.  
  91.                             for (int i = row; i >= 0; i -= 2)
  92.                             {
  93.                                 if (matrix[i][col] == ' ')
  94.                                 {
  95.                                     continue;
  96.                                 }
  97.                                 else
  98.                                 {
  99.                                     matrix[i][col] = ' ';
  100.                                     harmedCount++;
  101.                                 }
  102.                             }
  103.                         }
  104.                     }
  105.                     else if (direction == "down")
  106.                     {
  107.                         if (isInside(matrix, row, col))
  108.                         {
  109.                             Check(matrix, row, col);
  110.  
  111.                             for (int i = row; i < matrix.Length; i += 2)
  112.                             {
  113.                                 if (matrix[i][col] == ' ')
  114.                                 {
  115.                                     continue;
  116.                                 }
  117.                                 else
  118.                                 {
  119.                                     matrix[i][col] = ' ';
  120.                                     harmedCount++;
  121.                                 }
  122.                             }
  123.                         }
  124.                     }
  125.                     else if (direction == "right")
  126.                     {
  127.                         if (isInside(matrix, row, col))
  128.                         {
  129.                             Check(matrix, row, col);
  130.  
  131.                             for (int i = col; i < matrix[row].Length; i += 2)
  132.                             {
  133.                                 if (matrix[row][i] == ' ')
  134.                                 {
  135.                                     continue;
  136.                                 }
  137.                                 else
  138.                                 {
  139.                                     matrix[row][i] = ' ';
  140.                                     harmedCount++;
  141.                                 }
  142.                             }
  143.                         }
  144.                     }
  145.                     else if (direction == "left")
  146.                     {
  147.                         if (isInside(matrix, row, col))
  148.                         {
  149.                             Check(matrix, row, col);
  150.  
  151.                             for (int i = matrix[row].Length - 1; i >= 0; i -= 2)
  152.                             {
  153.                                 if (matrix[row][i] == ' ')
  154.                                 {
  155.                                     continue;
  156.                                 }
  157.                                 else
  158.                                 {
  159.                                     matrix[row][i] = ' ';
  160.                                     harmedCount++;
  161.                                 }
  162.                             }
  163.                         }  
  164.                     }
  165.                 }
  166.                 else
  167.                 {
  168.                     break;
  169.                 }
  170.             }
  171.  
  172.             Print(matrix);
  173.             Console.WriteLine($"Carrots: {carrotCount}");
  174.             Console.WriteLine($"Potatoes: {potatoCount}");
  175.             Console.WriteLine($"Lettuce: {lettuceCount}");
  176.             Console.WriteLine($"Harmed vegetables: {harmedCount}");
  177.         }
  178.  
  179.         private static void Print(char[][] matrix)
  180.         {
  181.             for (int i = 0; i < matrix.Length; i++)
  182.             {
  183.                 for (int j = 0; j < matrix[i].Length; j++)
  184.                 {
  185.                     Console.Write(matrix[i][j] + " ");
  186.                 }
  187.                 Console.WriteLine();
  188.             }
  189.         }
  190.  
  191.         private static bool isInside(char[][] matrix, int row, int col)
  192.         {
  193.             for (int i = 0; i < matrix.Length; i++)
  194.             {
  195.                 for (int j = 0; j < matrix[i].Length; j++)
  196.                 {
  197.                     if ((row >= 0 && row < matrix.Length) && (col >= 0 && col < matrix[row].Length))
  198.                     {
  199.                         return true;
  200.                     }
  201.                 }
  202.             }
  203.             return false;
  204.         }
  205.  
  206.         private static void Check(char[][] matrix, int row, int col)
  207.         {
  208.             if (matrix[row][col] == 'L')
  209.             {
  210.                 matrix[row][col] = 'M';
  211.             }
  212.             else if (matrix[row][col] == 'P')
  213.             {
  214.                 matrix[row][col] = 'M';
  215.             }
  216.             else if (matrix[row][col] == 'C')
  217.             {
  218.                 matrix[row][col] = 'M';
  219.             }
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement