Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Linq;
- namespace _02_Present_Delivery
- {
- class Program
- {
- static void Main(string[] args)
- {
- int m = int.Parse(Console.ReadLine());
- int n = int.Parse(Console.ReadLine());
- var matrix = new char[n, n];
- int rowSanta = 0;
- int colSanta = 0;
- int countOfNiceKids = 0;
- for (int row = 0; row < n; row++)
- {
- var input = Console.ReadLine()
- .Split(' ', StringSplitOptions.RemoveEmptyEntries)
- .Select(char.Parse)
- .ToArray();
- for (int col = 0; col < n; col++)
- {
- if (input[col] == 'S')
- {
- rowSanta = row;
- colSanta = col;
- }
- if (input[col] == 'V')
- {
- countOfNiceKids++;
- }
- matrix[row, col] = input[col];
- }
- }
- int comparisonForCount = countOfNiceKids;
- string command;
- while (m > 0)
- {
- if ((command = Console.ReadLine()) == "Christmas morning")
- {
- break;
- }
- matrix[rowSanta, colSanta] = '-';
- if (command == "up")
- {
- if (MatrixValidate(matrix, rowSanta - 1, colSanta) == true)
- {
- rowSanta = rowSanta - 1;
- MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
- }
- }
- else if (command == "down")
- {
- if (MatrixValidate(matrix, rowSanta + 1, colSanta) == true)
- {
- rowSanta = rowSanta + 1;
- MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
- }
- }
- else if (command == "left")
- {
- if (MatrixValidate(matrix, rowSanta, colSanta - 1) == true)
- {
- colSanta = colSanta - 1;
- MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
- }
- }
- else if (command == "right")
- {
- if (MatrixValidate(matrix, rowSanta, colSanta + 1) == true)
- {
- colSanta = colSanta + 1;
- MoveResult(ref countOfNiceKids, matrix, rowSanta, colSanta, ref m);
- }
- }
- matrix[rowSanta, colSanta] = 'S';
- }
- if (m == 0)
- {
- Console.WriteLine("Santa ran out of presents!");
- }
- MatrixPrint(matrix);
- if (countOfNiceKids==0)
- {
- Console.WriteLine($"Good job, Santa! {comparisonForCount} happy nice kid/s.");
- }
- else
- {
- Console.WriteLine($"No presents for {countOfNiceKids} nice kid/s.");
- }
- }
- private static void MatrixPrint(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();
- }
- }
- private static bool MatrixValidate (char[,] matrix, int rowSanta, int colSanta)
- {
- if (rowSanta>=0&&rowSanta<=matrix.GetLength(0)&&colSanta>=0&&colSanta<=matrix.GetLength(1))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private static void MoveResult(ref int countOfNiceKids, char[,] matrix, int rowSanta, int colSanta, ref int m)
- {
- char symbol = matrix[rowSanta, colSanta];
- if (symbol == 'V')
- {
- m--;
- countOfNiceKids--;
- }
- else if (symbol == 'C')
- {
- if (matrix[rowSanta - 1, colSanta] != '-' )
- {
- if (matrix[rowSanta - 1, colSanta] == 'V')
- {
- countOfNiceKids--;
- }
- matrix[rowSanta - 1, colSanta] = '-';
- m--;
- }
- if (matrix[rowSanta + 1, colSanta] != '-' )
- {
- if (matrix[rowSanta + 1, colSanta] == 'V')
- {
- countOfNiceKids--;
- }
- matrix[rowSanta + 1, colSanta] = '-';
- m--;
- }
- if (matrix[rowSanta, colSanta - 1] != '-' )
- {
- if (matrix[rowSanta, colSanta - 1] == 'V')
- {
- countOfNiceKids--;
- }
- matrix[rowSanta, colSanta - 1] = '-';
- m--;
- }
- if (matrix[rowSanta, colSanta + 1] != '-')
- {
- if (matrix[rowSanta, colSanta + 1] == 'V')
- {
- countOfNiceKids--;
- }
- matrix[rowSanta, colSanta + 1] = '-';
- m--;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement