Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _10._Radioactive_Mutant_Vampire_Bunnies
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] sizes = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- int n = sizes[0];
- int m = sizes[1];
- char[][] matrix = new char[n][];
- string startPosition = string.Empty;
- for (int row = 0; row < n; row++)
- {
- string inputLine = Console.ReadLine();
- matrix[row] = new char[m];
- for (int col = 0; col < m; col++)
- {
- matrix[row][col] = inputLine[col];
- if (matrix[row][col] == 'P')
- {
- startPosition = row + " " + col;
- }
- }
- }
- string commands = Console.ReadLine();
- string currentPosition = string.Empty;
- string status = string.Empty;
- for (int i = 0; i < commands.Length; i++)
- {
- if (i == 0)
- {
- currentPosition = startPosition;
- }
- char command = commands[i];
- currentPosition = PlayerMove(matrix, command, currentPosition);
- string[] tokens = currentPosition.Split().ToArray();
- int row = int.Parse(tokens[0]);
- int col = int.Parse(tokens[1]);
- if (tokens.Length == 3) //escape
- {
- status = "win";
- }
- matrix = BunniesMultiply(matrix);
- if (status == "win")
- {
- PrintMatrix(matrix);
- Console.WriteLine($"won: {row} {col}");
- break;
- }
- else // check dead
- {
- if (matrix[row][col] == 'B')
- {
- PrintMatrix(matrix);
- Console.WriteLine($"dead: {row} {col}");
- break;
- }
- }
- }
- }
- private static char[][] BunniesMultiply(char[][] matrix)
- {
- int rowCount = matrix.GetLength(0);
- int colCount = matrix[0].GetLength(0);
- char[][] resultingMatrix = new char[rowCount][];
- for (int row = 0; row < rowCount; row++)
- {
- resultingMatrix[row] = new char[colCount];
- for (int col = 0; col < colCount; col++)
- {
- if (matrix[row][col] == 'B')
- {
- resultingMatrix[row][col] = 'B';
- continue;
- }
- bool upLimit = row - 1 > -1 ? true : false;
- bool downLimit = row + 1 < rowCount ? true : false;
- bool rightLimit = col + 1 < colCount ? true : false;
- bool leftLimit = col - 1 > -1 ? true : false;
- if (upLimit)
- {
- if (matrix[row - 1][col] == 'B')
- {
- resultingMatrix[row][col] = 'B';
- continue;
- }
- }
- if (rightLimit)
- {
- if (matrix[row][col + 1] == 'B')
- {
- resultingMatrix[row][col] = 'B';
- continue;
- }
- }
- if (downLimit)
- {
- if (matrix[row + 1][col] == 'B')
- {
- resultingMatrix[row][col] = 'B';
- continue;
- }
- }
- if (leftLimit)
- {
- if (matrix[row][col - 1] == 'B')
- {
- resultingMatrix[row][col] = 'B';
- continue;
- }
- }
- resultingMatrix[row][col] = '.';
- }
- }
- return resultingMatrix;
- }
- private static void PrintMatrix(char[][] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix[0].GetLength(0); col++)
- {
- Console.Write(matrix[row][col]);
- }
- Console.WriteLine();
- }
- }
- private static string PlayerMove(char[][] matrix, char command, string currentPosition)
- {
- int rowCount = matrix.GetLength(0);
- int colCount = matrix[0].GetLength(0);
- string[] tokens = currentPosition.Split();
- int row = int.Parse(tokens[0]);
- int col = int.Parse(tokens[1]);
- bool upLimit = row - 1 > -1 ? true : false;
- bool downLimit = row + 1 < rowCount ? true : false;
- bool rightLimit = col + 1 < colCount ? true : false;
- bool leftLimit = col - 1 > -1 ? true : false;
- if (command == 'U')
- {
- if (upLimit == false)
- {
- currentPosition += " " + "win";
- }
- else
- {
- currentPosition = (row - 1) + " " + col;
- }
- }
- else if (command == 'R')
- {
- if (rightLimit == false)
- {
- currentPosition += " " + "win";
- }
- else
- {
- currentPosition = row + " " + (col + 1);
- }
- }
- else if (command == 'D')
- {
- if (downLimit == false)
- {
- currentPosition += " " + "win";
- }
- else
- {
- currentPosition = (row + 1) + " " + col;
- }
- }
- else if (command == 'L')
- {
- if (leftLimit == false)
- {
- currentPosition += " " + "win";
- }
- else
- {
- currentPosition = row + " " + (col - 1);
- }
- }
- return currentPosition;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement