Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- class Program
- {
- private static String[,] lair;
- private static bool isPlayerAlive = true;
- private static bool isPlayerEscape = false;
- static void Main(string[] args)
- {
- string[] tokens = Console.ReadLine().Split();
- int rows = int.Parse(tokens[0]);
- int cols = int.Parse(tokens[1]);
- lair = new String[rows, cols];
- int playerX = -1;
- int playerY = -1;
- for (int i = 0; i < rows; i++)
- {
- String currentLine = Console.ReadLine();
- for (int j = 0; j < currentLine.Length; j++)
- {
- if (currentLine[j] == 'P')
- {
- playerX = i;
- playerY = j;
- }
- if (currentLine[j] == 'B')
- {
- lair[i, j] = "1";
- }
- else
- {
- lair[i, j] = currentLine[j].ToString();
- }
- }
- }
- string directions = Console.ReadLine();
- int currentStep = 1;
- for (int i = 0; i < directions.Length; i++)
- {
- //Move the player
- char currentDirection = directions[i];
- int nextPlayerX = playerX;
- int nextPlayerY = playerY;
- switch (currentDirection)
- {
- case 'D': nextPlayerX++; break;
- case 'U': nextPlayerX--; break;
- case 'L': nextPlayerY--; break;
- case 'R': nextPlayerY++; break;
- }
- lair[playerX, playerY] = ".";
- if (!IsValidCell(nextPlayerX, nextPlayerY))
- {
- isPlayerEscape = true;
- }
- else if (lair[nextPlayerX, nextPlayerY] != ".")
- {
- isPlayerAlive = false;
- }
- else
- {
- lair[nextPlayerX, nextPlayerY] = "P";
- }
- playerX = nextPlayerX;
- playerY = nextPlayerY;
- //Move the bunnies
- for (int row = 0; row < rows; row++)
- {
- for (int col = 0; col < cols; col++)
- {
- if (lair[row, col].ToString() == currentStep.ToString())
- {
- MarkNeighbours(row, col, currentStep + 1);
- }
- }
- }
- currentStep++;
- if (!isPlayerAlive || isPlayerEscape)
- {
- break;
- }
- }
- for (int row = 0; row < lair.GetLength(0); row++)
- {
- for (int col = 0; col < lair.GetLength(1); col++)
- {
- if (lair[row, col] != ".")
- {
- lair[row, col] = "B";
- }
- }
- }
- PrintLair();
- if (playerY == -1) playerY = 0;
- if (playerY == lair.GetLength(1)) playerY--;
- if (playerX == -1) playerX = 0;
- if (playerX == lair.GetLength(0)) playerX--;
- if (isPlayerEscape)
- {
- Console.WriteLine("won: {0} {1}", playerX, playerY);
- }
- else
- {
- Console.WriteLine("dead: {0} {1}", playerX, playerY);
- }
- }
- private static void PrintLair()
- {
- for (int row = 0; row < lair.GetLength(0); row++)
- {
- for (int col = 0; col < lair.GetLength(1); col++)
- {
- Console.Write(lair[row, col]);
- }
- Console.WriteLine();
- }
- }
- private static void MarkNeighbours(int row, int col, int currentStep)
- {
- if(IsValidCell(row + 1, col))
- {
- if(lair[row + 1, col] == "P")
- {
- isPlayerAlive = false;
- }
- lair[row + 1, col] = currentStep.ToString();
- }
- if(IsValidCell(row, col + 1))
- {
- if(lair[row, col + 1] == "P")
- {
- isPlayerAlive = false;
- }
- lair[row, col + 1] = currentStep.ToString();
- }
- if(IsValidCell(row - 1, col))
- {
- if(lair[row - 1, col] == "P")
- {
- isPlayerAlive = false;
- }
- lair[row - 1, col] = currentStep.ToString();
- }
- if(IsValidCell(row, col - 1))
- {
- if(lair[row, col - 1] == "P")
- {
- isPlayerAlive = false;
- }
- lair[row, col - 1] = currentStep.ToString();
- }
- }
- private static bool IsValidCell(int x, int y)
- {
- return x >= 0 && x < lair.GetLength(0) && y >= 0 && y < lair.GetLength(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement