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.BeaverAtWork
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[,] matrix = new char[n, n];
- int beaverRow = -1;
- int beaverCol = -1;
- int woodBranchesCount = 0;
- Stack<char> woodBranches = new Stack<char>();
- for (int row = 0; row < n; row++)
- {
- char[] colElements = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
- for (int col = 0; col < n; col++)
- {
- matrix[row, col] = colElements[col];
- if (matrix[row, col] == 'B')
- {
- beaverRow = row;
- beaverCol = col;
- }
- if (Char.IsLower(matrix[row, col]))
- {
- woodBranchesCount++;
- }
- }
- }
- string command = Console.ReadLine();
- while (command != "end")
- {
- switch (command)
- {
- case "up":
- matrix[beaverRow, beaverCol] = '-';
- beaverRow--;
- if (!IsInside(matrix, beaverRow, beaverCol))
- {
- beaverRow++;
- matrix[beaverRow, beaverCol] = 'B';
- if (woodBranches.Any())
- {
- woodBranches.Pop();
- }
- }
- else
- {
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- else if (matrix[beaverRow, beaverCol] == 'F')
- {
- matrix[beaverRow, beaverCol] = '-';
- if (beaverRow != 0)
- {
- beaverRow = 0;
- }
- else
- {
- beaverRow = n - 1;
- }
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- }
- }
- break;
- case "down":
- matrix[beaverRow, beaverCol] = '-';
- beaverRow++;
- if (!IsInside(matrix, beaverRow, beaverCol))
- {
- beaverRow--;
- matrix[beaverRow, beaverCol] = 'B';
- if (woodBranches.Any())
- {
- woodBranches.Pop();
- }
- }
- else
- {
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- else if (matrix[beaverRow, beaverCol] == 'F')
- {
- matrix[beaverRow, beaverCol] = '-';
- if (beaverRow != n - 1)
- {
- beaverRow = n - 1;
- }
- else
- {
- beaverRow = 0;
- }
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- }
- }
- break;
- case "left":
- matrix[beaverRow, beaverCol] = '-';
- beaverCol--;
- if (!IsInside(matrix, beaverRow, beaverCol))
- {
- beaverCol++;
- matrix[beaverRow, beaverCol] = 'B';
- if (woodBranches.Any())
- {
- woodBranches.Pop();
- }
- }
- else
- {
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- else if (matrix[beaverRow, beaverCol] == 'F')
- {
- matrix[beaverRow, beaverCol] = '-';
- if (beaverCol != 0)
- {
- beaverCol = 0;
- }
- else
- {
- beaverCol = n - 1;
- }
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- }
- }
- break;
- case "right":
- matrix[beaverRow, beaverCol] = '-';
- beaverCol++;
- if (!IsInside(matrix, beaverRow, beaverCol))
- {
- beaverCol--;
- matrix[beaverRow, beaverCol] = 'B';
- if (woodBranches.Any())
- {
- woodBranches.Pop();
- }
- }
- else
- {
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- else if (matrix[beaverRow, beaverCol] == 'F')
- {
- matrix[beaverRow, beaverCol] = '-';
- if (beaverCol != n - 1)
- {
- beaverCol = n - 1;
- }
- else
- {
- beaverCol = 0;
- }
- if (Char.IsLower(matrix[beaverRow, beaverCol]))
- {
- woodBranches.Push(matrix[beaverRow, beaverCol]);
- woodBranchesCount--;
- matrix[beaverRow, beaverCol] = 'B';
- }
- }
- }
- break;
- }
- if (woodBranchesCount == 0)
- {
- Stack<char> reversedWoodBranches = new Stack<char>();
- while (woodBranches.Any())
- {
- reversedWoodBranches.Push(woodBranches.Pop());
- }
- Console.WriteLine($"The Beaver successfully collect {reversedWoodBranches.Count} wood branches: {string.Join(", ", reversedWoodBranches)}.");
- PrintMatrix(matrix);
- return;
- }
- command = Console.ReadLine();
- }
- Console.WriteLine($"The Beaver failed to collect every wood branch. There are {woodBranches.Count} branches left.");
- PrintMatrix(matrix);
- }
- private static bool IsInside(char[,] matrix, int row, int col)
- {
- return row >= 0 && row < matrix.GetLength(0) && col >= 0 && col < matrix.GetLength(1);
- }
- private static void PrintMatrix(char[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write($"{matrix[row, col]} ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement