Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _02_PresentDelivery
- {
- class Program
- {
- static void Main(string[] args)
- {
- int presentsCount = int.Parse(Console.ReadLine());
- int size = int.Parse(Console.ReadLine());
- string[,] matrix = new string[size, size];
- int santaRow = -1;
- int santaCol = -1;
- int goodKids = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- string[] currRow = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries);
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (currRow[col].ToString() == "S")
- {
- santaRow = row;
- santaCol = col;
- }
- else if (currRow[col].ToString() == "V")
- {
- goodKids++;
- }
- matrix[row, col] = currRow[col];
- }
- }
- string command = "";
- while ((command = Console.ReadLine()) != "Christmas morning" && presentsCount > 0)
- {
- matrix[santaRow, santaCol] = "-";
- if (command == "up")
- {
- santaRow--;
- }
- else if (command == "down")
- {
- santaRow++;
- }
- else if (command == "left")
- {
- santaCol--;
- }
- else if (command == "right")
- {
- santaCol++;
- }
- if (CheckBounderies(matrix, santaRow, santaCol))
- {
- Console.WriteLine("Santa ran out of presents.");
- break;
- }
- if (matrix[santaRow, santaCol] == "-")
- {
- matrix[santaRow, santaCol] = "S";
- }
- else if (matrix[santaRow, santaCol] == "V")
- {
- matrix[santaRow, santaCol] = "S";
- presentsCount--;
- if (presentsCount == 0)
- {
- Console.WriteLine("Santa ran out of presents!");
- break;
- }
- }
- else if (matrix[santaRow, santaCol] == "X")
- {
- continue;
- }
- else if (matrix[santaRow, santaCol] == "C")
- {
- matrix[santaRow, santaCol] = "S";
- if (matrix[santaRow, santaCol + 1] != "-")// right
- {
- presentsCount--;
- matrix[santaRow, santaCol + 1] = "-";
- if (presentsCount == 0)
- {
- break;
- }
- }
- if (matrix[santaRow, santaCol - 1] != "-")// left
- {
- presentsCount--;
- matrix[santaRow, santaCol - 1] = "-";
- if (presentsCount == 0)
- {
- break;
- }
- }
- if (matrix[santaRow - 1, santaCol] != "-")// up
- {
- presentsCount--;
- matrix[santaRow - 1, santaCol] = "-";
- if (presentsCount == 0)
- {
- break;
- }
- }
- if (matrix[santaRow + 1, santaCol] == "V")// down
- {
- presentsCount--;
- matrix[santaRow + 1, santaCol] = "-";
- if (presentsCount == 0)
- {
- break;
- }
- }
- }
- }
- int goodKidsLeft = 0;
- foreach (var item in matrix)
- {
- if (item == "V")
- {
- goodKidsLeft++;
- }
- }
- PrintMatrix(matrix);
- if (goodKidsLeft == 0)
- {
- Console.WriteLine($"Good job, Santa! {goodKids} happy nice kid/s.");
- }
- else
- {
- Console.WriteLine($"No presents for {goodKidsLeft} nice kid/s.");
- }
- }
- static bool CheckBounderies(string[,] matrix, int santaRow, int santaCol)
- {
- return santaRow < 0 || santaRow >= matrix.GetLength(0) || santaCol < 0 || santaCol >= matrix.GetLength(1);
- }
- private static void PrintMatrix(string[,] 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();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment