Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _02.SeashellTreasure
- {
- class Program
- {
- static int stolenShells;
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[][] beach = new char[n][];
- List<char> collectedShells = new List<char>();
- stolenShells = 0;
- for (int row = 0; row < n; row++)
- {
- char[] currentRowElements = Console.ReadLine().Split(' ').Select(char.Parse).ToArray(); ;
- beach[row] = currentRowElements;
- }
- string[] input = Console.ReadLine().Split();
- string command = input[0];
- while (command != "Sunset")
- {
- int currentRow = int.Parse(input[1]);
- int currentCol = int.Parse(input[2]);
- if (command == "Collect")
- {
- if (!IndexIsValid(currentRow, currentCol, beach))
- {
- input = Console.ReadLine().Split();
- command = input[0];
- continue;
- }
- char symbol = beach[currentRow][currentCol];
- CollectSymbolIfThereIsOne(beach, collectedShells, currentRow, currentCol, symbol);
- }
- else if (command == "Steal")
- {
- string direction = input[3];
- if (!IndexIsValid(currentRow, currentCol, beach))
- {
- input = Console.ReadLine().Split();
- command = input[0];
- continue;
- }
- char symbol = beach[currentRow][currentCol];
- if (direction == "up")
- {
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- for (int i = 1; i <= 3; i++)
- {
- if (IndexIsValid(currentRow - 1, currentCol, beach))
- {
- currentRow--;
- char nextSymbol = beach[currentRow][currentCol];
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- }
- }
- }
- else if (direction == "down")
- {
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- for (int i = 1; i <= 3; i++)
- {
- if (IndexIsValid(currentRow+1, currentCol, beach))
- {
- currentRow++;
- char nextSymbol = beach[currentRow][currentCol];
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- }
- }
- }
- else if (direction == "left")
- {
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- for (int i = 1; i <= 3; i++)
- {
- if (IndexIsValid(currentRow, currentCol-1, beach))
- {
- currentCol--;
- char nextSymbol = beach[currentRow][currentCol];
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- }
- }
- }
- else if (direction == "right")
- {
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- for (int i = 1; i <= 3; i++)
- {
- if (IndexIsValid(currentRow, currentCol+1, beach))
- {
- currentCol++;
- char nextSymbol = beach[currentRow][currentCol];
- StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
- }
- }
- }
- }
- input = Console.ReadLine().Split();
- command = input[0];
- }
- foreach (var row in beach)
- {
- Console.WriteLine(string.Join(" ", row));
- }
- if(collectedShells.Count == 0)
- {
- Console.WriteLine($"Collected seashells: {collectedShells.Count}");
- }
- else
- {
- Console.WriteLine($"Collected seashells: {collectedShells.Count} -> "
- + string.Join(", ", collectedShells));
- }
- Console.WriteLine($"Stolen seashells: {stolenShells}");
- }
- private static void CollectSymbolIfThereIsOne(char[][] beach, List<char> collectedShells, int currentRow, int currentCol, char symbol)
- {
- if (symbol == 'C' || symbol == 'M' || symbol == 'N')
- {
- collectedShells.Add(symbol);
- beach[currentRow][currentCol] = '-';
- }
- }
- private static void StoleSymbolIfThereIsOne(char[][] beach, int currentRow, int currentCol, char symbol)
- {
- if (symbol == 'C' || symbol == 'M' || symbol == 'N')
- {
- stolenShells++;
- beach[currentRow][currentCol] = '-';
- }
- }
- public static bool IndexIsValid(int row, int col, char[][] beach)
- {
- return
- row >= 0 &&
- row < beach.Length &&
- col >= 0 &&
- col < beach[row].Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement