Advertisement
Iv555

Untitled

Feb 21st, 2022
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace T02BeaverAtWork
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             int size = int.Parse(Console.ReadLine());
  14.             char[,] matrix = new char[size, size];
  15.  
  16.             int beaverRow = 0;
  17.             int beaverCol = 0;
  18.             int countOfWoods = 0;
  19.             for (int i = 0; i < matrix.GetLength(0); i++)
  20.             {
  21.                 char[] currRow = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(char.Parse)
  22.                     .ToArray();
  23.                 for (int j = 0; j < matrix.GetLength(1); j++)
  24.                 {
  25.                     matrix[i, j] = currRow[j];
  26.                     if (matrix[i, j] == 'B')
  27.                     {
  28.                         beaverRow = i;
  29.                         beaverCol = j;
  30.                     }
  31.  
  32.                     if (Char.IsLower(matrix[i, j]))
  33.                     {
  34.                         countOfWoods++;
  35.                     }
  36.  
  37.                 }
  38.             }
  39.  
  40.             int initialCountOfWoods = countOfWoods;
  41.             string command;
  42.             List<char> collectedWoods = new List<char>();
  43.             while ((command = Console.ReadLine()) != "end")
  44.             {
  45.                 int oldBeaverRow = beaverRow;
  46.                 int oldbeaverCol = beaverCol;
  47.                 matrix[beaverRow, beaverCol] = '-';
  48.                 if (command == "up")
  49.                 {
  50.                     beaverRow--;
  51.                 }
  52.                 else if (command == "down")
  53.                 {
  54.                     beaverRow++;
  55.                 }
  56.                 else if (command == "left")
  57.                 {
  58.                     beaverCol--;
  59.                 }
  60.                 else if (command == "right")
  61.                 {
  62.                     beaverCol++;
  63.                 }
  64.  
  65.                 if (!IsWIthinMatrix(matrix, beaverRow, beaverCol))
  66.                 {
  67.                     if (collectedWoods.Count > 0)
  68.                     {
  69.                         collectedWoods.RemoveAt(collectedWoods.Count - 1);
  70.                         initialCountOfWoods--;
  71.                         //if (countOfWoods == 0)
  72.                         //{
  73.                         //    break;
  74.                         //}
  75.  
  76.                     }
  77.  
  78.                     beaverRow = oldBeaverRow;
  79.                     beaverCol = oldbeaverCol;
  80.                     matrix[beaverRow, beaverCol] = 'B';
  81.                 }
  82.                 else if (IsWIthinMatrix(matrix, beaverRow, beaverCol))
  83.                 {
  84.                     if (Char.IsLower(matrix[beaverRow, beaverCol]))
  85.                     {
  86.                         collectedWoods.Add(matrix[beaverRow, beaverCol]);
  87.                         countOfWoods--;
  88.                         matrix[beaverRow, beaverCol] = 'B';
  89.                         if (countOfWoods == 0)
  90.                         {
  91.                             break;
  92.                         }
  93.  
  94.                     }
  95.                     else if (matrix[beaverRow, beaverCol] == 'F' && beaverRow != 0 && beaverCol != 0 &&
  96.                              beaverRow != matrix.GetLength(0) - 1 && beaverCol != matrix.GetLength(1))
  97.                     {
  98.                         matrix[beaverRow, beaverCol] = '-';
  99.                         MovesToEnd(matrix, command, ref beaverRow, ref beaverCol);
  100.                     }
  101.                     else if (matrix[beaverRow, beaverCol] == 'F' && (beaverRow == 0 || beaverCol == 0 ||
  102.                                                                      beaverRow == matrix.GetLength(0) - 1 ||
  103.                                                                      beaverCol == matrix.GetLength(1)))
  104.                     {
  105.                         matrix[beaverRow, beaverCol] = '-';
  106.                         SwimsOppositeEnd(matrix, command, ref beaverRow, ref beaverCol);
  107.                         if (Char.IsLetter(matrix[beaverRow, beaverCol]))
  108.                         {
  109.                             collectedWoods.Add(matrix[beaverRow, beaverCol]);
  110.                         }
  111.                     }
  112.  
  113.                     matrix[beaverRow, beaverCol] = 'B';
  114.                 }
  115.             }
  116.  
  117.             if (initialCountOfWoods == collectedWoods.Count)
  118.             {
  119.                 Console.WriteLine(
  120.                     $"The Beaver successfully collect {collectedWoods.Count} wood branches: {string.Join(", ", collectedWoods)}.");
  121.             }
  122.             else
  123.             {
  124.                 Console.WriteLine(
  125.                     $"The Beaver failed to collect every wood branch. There are {initialCountOfWoods - collectedWoods.Count} branches left.");
  126.             }
  127.  
  128.             PrintMatrix(matrix);
  129.  
  130.         }
  131.  
  132.         private static void SwimsOppositeEnd(char[,] matrix, string command, ref int beaverRow, ref int beaverCol)
  133.         {
  134.            
  135.             if (command == "up")
  136.             {
  137.                 beaverRow = matrix.GetLength(0) - 1;
  138.             }
  139.             else if (command == "down")
  140.             {
  141.                 beaverRow = 0;
  142.             }
  143.             else if (command == "right")
  144.             {
  145.                 beaverCol = matrix.GetLength(1) - 1;
  146.             }
  147.             else if (command == "left")
  148.             {
  149.                 beaverCol = 0;
  150.             }
  151.         }
  152.  
  153.         private static void MovesToEnd(char[,] matrix, string command, ref int beaverRow, ref int beaverCol)
  154.         {
  155.             if (command == "up")
  156.             {
  157.                 beaverRow = 0;
  158.             }
  159.             else if (command == "down")
  160.             {
  161.                 beaverRow = matrix.GetLength(0) - 1;
  162.             }
  163.             else if (command == "left")
  164.             {
  165.                 beaverCol = 0;
  166.             }
  167.             else if (command == "right")
  168.             {
  169.                 beaverCol = matrix.GetLength(1) - 1;
  170.             }
  171.         }
  172.  
  173.         public static bool IsWIthinMatrix(char[,] matrix, int row, int col)
  174.             => row >= 0 && row < matrix.GetLength(0) && col >= 0 && col < matrix.GetLength(1);
  175.  
  176.         public static void PrintMatrix(char[,] matrix)
  177.         {
  178.             for (int i = 0; i < matrix.GetLength(0); i++)
  179.             {
  180.                 for (int j = 0; j < matrix.GetLength(1); j++)
  181.                 {
  182.                     Console.Write(matrix[i, j] + " ");
  183.                 }
  184.  
  185.                 Console.WriteLine();
  186.             }
  187.  
  188.         }
  189.     }
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement