Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _01Garden
- {
- class Program
- {
- static void Main()
- {
- int rows = int.Parse(Console.ReadLine());
- char[][] array = new char[rows][];
- int harmed = 0;
- int lettuce = 0;
- int carrot = 0;
- int patoto = 0;
- for (int i = 0; i < rows; i++)
- {
- array[i] = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(char.Parse)
- .ToArray();
- }
- string command = string.Empty;
- while ((command = Console.ReadLine().ToLower()) != "end of harvest")
- {
- var tokens = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- string action = tokens[0];
- int row = int.Parse(tokens[1]);
- int col = int.Parse(tokens[2]);
- switch (action)
- {
- case "harvest":
- if (IsInside(array, row, col))
- {
- if (array[row][col] == 'L')
- {
- array[row][col] = ' ';
- lettuce++;
- }
- else if (array[row][col] == 'P')
- {
- array[row][col] = ' ';
- patoto++;
- }
- else if (array[row][col] == 'C')
- {
- array[row][col] = ' ';
- carrot++;
- }
- }
- break;
- case "mole":
- string direction = tokens[3].ToLower();
- harmed += Annihilation(array, row, col, direction);
- break;
- default:
- break;
- }
- }
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write(string.Join(" ", array[i]));
- Console.WriteLine();
- }
- Console.WriteLine($"Carrots: {carrot}");
- Console.WriteLine($"Potatoes: {patoto}");
- Console.WriteLine($"Lettuce: {lettuce}");
- Console.WriteLine($"Harmed vegetables: {harmed}");
- }
- public static bool IsInside(char[][] matrix, int row, int col)
- {
- return row >= 0 && col >= 0 && row < matrix.Length && col < matrix[row].Length;
- }
- public static int Annihilation(char[][] arr, int row, int col, string direction)
- {
- int counter = 0;
- if (!IsInside(arr, row, col))
- {
- return counter;
- }
- if (arr[row][col] != ' ')
- {
- arr[row][col] = ' ';
- counter++;
- }
- if (direction == "up")
- {
- for (int i = row; i >= 0; i -= 2)
- {
- if (IsInside(arr, i, col) && arr[i][col] != ' ')
- {
- arr[i][col] = ' ';
- counter++;
- }
- }
- }
- else if (direction == "down")
- {
- for (int i = row; i < arr.Length; i += 2)
- {
- if (IsInside(arr, i, col) && arr[i][col] != ' ')
- {
- arr[i][col] = ' ';
- counter++;
- }
- }
- }
- else if (direction == "right")
- {
- for (int i = col; i < arr[row].Length; i += 2)
- {
- if (IsInside(arr, row, i) && arr[row][i] != ' ')
- {
- arr[col][i] = ' ';
- counter++;
- }
- }
- }
- else if (direction == "left")
- {
- for (int i = col; i >= 0; i -= 2)
- {
- if (IsInside(arr, row, i) && arr[row][i] != ' ')
- {
- arr[col][i] = ' ';
- counter++;
- }
- }
- }
- return counter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement