Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- internal class Bunnies
- {
- private static char[,] bunnyLair;
- private static void Main()
- {
- int[] dimensions = Console.ReadLine()
- .Split(' ')
- .Select(int.Parse)
- .ToArray();
- int rowCount = dimensions[0];
- int colCount = dimensions[1];
- bunnyLair = new char[rowCount, colCount];
- int rowOfPerson = 0;
- int colOfPerson = 0;
- for (int i = 0; i < bunnyLair.GetLength(0); i++)
- {
- string currentLine = Console.ReadLine();
- for (int j = 0; j < bunnyLair.GetLength(1); j++)
- {
- if (currentLine[j] == 'P')
- {
- rowOfPerson = i;
- colOfPerson = j;
- }
- bunnyLair[i, j] = currentLine[j];
- }
- }
- string lineOfCommands = Console.ReadLine();
- string situation = "";
- for (int i = 0; i < lineOfCommands.Length; i++)
- {
- situation = MovePerson(lineOfCommands[i], rowOfPerson, colOfPerson, situation);
- if (situation == "Won"
- || situation == "Lost")
- {
- if (situation == "Lost")
- {
- rowOfPerson = UpdateRow(lineOfCommands[i], rowOfPerson);
- colOfPerson = UpdateCol(lineOfCommands[i], colOfPerson);
- }
- situation = MultiplyBunnies(situation);
- break;
- }
- rowOfPerson = UpdateRow(lineOfCommands[i], rowOfPerson);
- colOfPerson = UpdateCol(lineOfCommands[i], colOfPerson);
- situation = MultiplyBunnies(situation);
- if (situation == "Lost")
- {
- break;
- }
- }
- if (situation == "Won" ||
- situation == "Neutral")
- {
- PrintLair(bunnyLair);
- Console.WriteLine("won: {0} {1}", rowOfPerson, colOfPerson);
- }
- else
- {
- PrintLair(bunnyLair);
- Console.WriteLine("dead: {0} {1}", rowOfPerson, colOfPerson);
- }
- }
- private static string MovePerson(
- char currentCommand, int rowOfPerson, int colOfPerson, string situation)
- {
- try
- {
- switch (currentCommand)
- {
- case 'U':
- if (bunnyLair[rowOfPerson - 1, colOfPerson] == 'B')
- {
- return situation = "Lost";
- }
- bunnyLair[rowOfPerson - 1, colOfPerson] = 'P';
- bunnyLair[rowOfPerson, colOfPerson] = '.';
- break;
- case 'D':
- if (bunnyLair[rowOfPerson + 1, colOfPerson] == 'B')
- {
- return situation = "Lost";
- }
- bunnyLair[rowOfPerson + 1, colOfPerson] = 'P';
- bunnyLair[rowOfPerson, colOfPerson] = '.';
- break;
- case 'L':
- if (bunnyLair[rowOfPerson, colOfPerson - 1] == 'B')
- {
- return situation = "Lost";
- }
- bunnyLair[rowOfPerson, colOfPerson - 1] = 'P';
- bunnyLair[rowOfPerson, colOfPerson] = '.';
- break;
- case 'R':
- if (bunnyLair[rowOfPerson, colOfPerson + 1] == 'B')
- {
- return situation = "Lost";
- }
- bunnyLair[rowOfPerson, colOfPerson + 1] = 'P';
- bunnyLair[rowOfPerson, colOfPerson] = '.';
- break;
- }
- return situation = "Neutral";
- }
- catch (Exception)
- {
- bunnyLair[rowOfPerson, colOfPerson] = '.';
- return situation = "Won";
- }
- }
- private static string MultiplyBunnies(string situation)
- {
- int rowsLength = bunnyLair.GetLength(0);
- int colsLength = bunnyLair.GetLength(1);
- bool[,] mask = new bool[rowsLength, colsLength];
- // Set new positions in mask
- for (int i = 0; i < rowsLength; i++)
- {
- for (int j = 0; j < colsLength; j++)
- {
- if (bunnyLair[i, j] == 'B')
- {
- situation = SpreadBunnie(i, j, mask, rowsLength, colsLength, situation);
- }
- }
- }
- // Apply mask
- for (int i = 0; i < rowsLength; i++)
- {
- for (int j = 0; j < colsLength; j++)
- {
- if (mask[i, j])
- {
- bunnyLair[i, j] = 'B';
- }
- }
- }
- if (situation == "Lost")
- {
- return situation;
- }
- return "Neutral";
- }
- private static string SpreadBunnie(
- int i, int j, bool[,] mask, int rowsLength, int colsLength, string situation)
- {
- if (IsWithinField(i + 1, j, rowsLength, colsLength))
- {
- if (bunnyLair[i + 1, j] == 'P')
- {
- situation = "Lost";
- }
- mask[i + 1, j] = true;
- }
- if (IsWithinField(i - 1, j, rowsLength, colsLength))
- {
- if (bunnyLair[i - 1, j] == 'P')
- {
- situation = "Lost";
- }
- mask[i - 1, j] = true;
- }
- if (IsWithinField(i, j + 1, rowsLength, colsLength))
- {
- if (bunnyLair[i, j + 1] == 'P')
- {
- situation = "Lost";
- }
- mask[i, j + 1] = true;
- }
- if (IsWithinField(i, j - 1, rowsLength, colsLength))
- {
- if (bunnyLair[i, j - 1] == 'P')
- {
- situation = "Lost";
- }
- mask[i, j - 1] = true;
- }
- return situation;
- }
- private static int UpdateRow(char currentCommand, int rowOfPerson)
- {
- switch (currentCommand)
- {
- case 'U':
- rowOfPerson -= 1;
- break;
- case 'D':
- rowOfPerson += 1;
- break;
- }
- return rowOfPerson;
- }
- private static int UpdateCol(char currentCommand, int colOfPerson)
- {
- switch (currentCommand)
- {
- case 'L':
- colOfPerson -= 1;
- break;
- case 'R':
- colOfPerson += 1;
- break;
- }
- return colOfPerson;
- }
- private static void PrintLair(char[,] bunnyLair)
- {
- for (int i = 0; i < bunnyLair.GetLength(0); i++)
- {
- for (int j = 0; j < bunnyLair.GetLength(1); j++)
- {
- Console.Write(bunnyLair[i, j]);
- }
- Console.WriteLine();
- }
- }
- private static bool IsWithinField(int row, int col, int totalRows, int totalCols)
- {
- bool isWithin =
- 0 <= row && row < totalRows &&
- 0 <= col && col < totalCols;
- return isWithin;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement