Advertisement
Guest User

Untitled

a guest
Oct 11th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6.     private static String[,] lair;
  7.     private static bool isPlayerAlive = true;
  8.     private static bool isPlayerEscape = false;
  9.  
  10.     static void Main(string[] args)
  11.     {
  12.         string[] tokens = Console.ReadLine().Split();
  13.         int rows = int.Parse(tokens[0]);
  14.         int cols = int.Parse(tokens[1]);
  15.  
  16.         lair = new String[rows, cols];
  17.  
  18.         int playerX = -1;
  19.         int playerY = -1;
  20.  
  21.         for (int i = 0; i < rows; i++)
  22.         {
  23.             String currentLine = Console.ReadLine();
  24.             for (int j = 0; j < currentLine.Length; j++)
  25.             {
  26.                 if (currentLine[j] == 'P')
  27.                 {
  28.                     playerX = i;
  29.                     playerY = j;
  30.                 }
  31.  
  32.                 if (currentLine[j] == 'B')
  33.                 {
  34.                     lair[i, j] = "1";
  35.                 }
  36.                 else
  37.                 {
  38.                     lair[i, j] = currentLine[j].ToString();
  39.                 }
  40.             }
  41.         }
  42.  
  43.         string directions = Console.ReadLine();
  44.         int currentStep = 1;
  45.  
  46.         for (int i = 0; i < directions.Length; i++)
  47.         {
  48.             //Move the player
  49.             char currentDirection = directions[i];
  50.  
  51.             int nextPlayerX = playerX;
  52.             int nextPlayerY = playerY;
  53.  
  54.             switch (currentDirection)
  55.             {
  56.                 case 'D': nextPlayerX++; break;
  57.                 case 'U': nextPlayerX--; break;
  58.                 case 'L': nextPlayerY--; break;
  59.                 case 'R': nextPlayerY++; break;
  60.             }
  61.  
  62.             lair[playerX, playerY] = ".";
  63.  
  64.             if (!IsValidCell(nextPlayerX, nextPlayerY))
  65.             {
  66.                 isPlayerEscape = true;
  67.             }
  68.             else if (lair[nextPlayerX, nextPlayerY] != ".")
  69.             {
  70.                 isPlayerAlive = false;
  71.             }
  72.             else
  73.             {
  74.                 lair[nextPlayerX, nextPlayerY] = "P";
  75.             }
  76.  
  77.             playerX = nextPlayerX;
  78.             playerY = nextPlayerY;
  79.  
  80.             //Move the bunnies
  81.             for (int row = 0; row < rows; row++)
  82.             {
  83.                 for (int col = 0; col < cols; col++)
  84.                 {
  85.                     if (lair[row, col].ToString() == currentStep.ToString())
  86.                     {
  87.                         MarkNeighbours(row, col, currentStep + 1);
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             currentStep++;
  93.  
  94.             if (!isPlayerAlive || isPlayerEscape)
  95.             {
  96.                 break;
  97.             }
  98.         }
  99.  
  100.         for (int row = 0; row < lair.GetLength(0); row++)
  101.         {
  102.             for (int col = 0; col < lair.GetLength(1); col++)
  103.             {
  104.                 if (lair[row, col] != ".")
  105.                 {
  106.                     lair[row, col] = "B";
  107.                 }
  108.             }
  109.         }
  110.  
  111.         PrintLair();
  112.  
  113.         if (playerY == -1) playerY = 0;
  114.         if (playerY == lair.GetLength(1)) playerY--;
  115.         if (playerX == -1) playerX = 0;
  116.         if (playerX == lair.GetLength(0)) playerX--;
  117.  
  118.         if (isPlayerEscape)
  119.         {
  120.             Console.WriteLine("won: {0} {1}", playerX, playerY);
  121.         }
  122.         else
  123.         {
  124.             Console.WriteLine("dead: {0} {1}", playerX, playerY);
  125.         }
  126.     }
  127.  
  128.     private static void PrintLair()
  129.     {
  130.         for (int row = 0; row < lair.GetLength(0); row++)
  131.         {
  132.             for (int col = 0; col < lair.GetLength(1); col++)
  133.             {
  134.                 Console.Write(lair[row, col]);
  135.             }
  136.             Console.WriteLine();
  137.         }
  138.     }
  139.  
  140.     private static void MarkNeighbours(int row, int col, int currentStep)
  141.     {
  142.         if(IsValidCell(row + 1, col))
  143.         {
  144.             if(lair[row + 1, col] == "P")
  145.             {
  146.                 isPlayerAlive = false;
  147.             }
  148.  
  149.             lair[row + 1, col] = currentStep.ToString();
  150.         }
  151.  
  152.         if(IsValidCell(row, col + 1))
  153.         {
  154.             if(lair[row, col + 1] == "P")
  155.             {
  156.                 isPlayerAlive = false;
  157.             }
  158.  
  159.             lair[row, col + 1] = currentStep.ToString();
  160.         }
  161.  
  162.         if(IsValidCell(row - 1, col))
  163.         {
  164.             if(lair[row - 1, col] == "P")
  165.             {
  166.                 isPlayerAlive = false;
  167.             }
  168.  
  169.             lair[row - 1, col] = currentStep.ToString();
  170.         }
  171.  
  172.         if(IsValidCell(row, col - 1))
  173.         {
  174.             if(lair[row, col - 1] == "P")
  175.             {
  176.                 isPlayerAlive = false;
  177.             }
  178.  
  179.             lair[row, col - 1] = currentStep.ToString();
  180.         }
  181.     }
  182.  
  183.     private static bool IsValidCell(int x, int y)
  184.     {
  185.         return x >= 0 && x < lair.GetLength(0) && y >= 0 && y < lair.GetLength(1);
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement