Advertisement
kidroca

Radioactive Bunnies

Oct 11th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. internal class Bunnies
  7. {
  8.     private static char[,] bunnyLair;
  9.  
  10.     private static void Main()
  11.     {
  12.         int[] dimensions = Console.ReadLine()
  13.             .Split(' ')
  14.             .Select(int.Parse)
  15.             .ToArray();
  16.  
  17.         int rowCount = dimensions[0];
  18.         int colCount = dimensions[1];
  19.  
  20.         bunnyLair = new char[rowCount, colCount];
  21.  
  22.         int rowOfPerson = 0;
  23.         int colOfPerson = 0;
  24.  
  25.         for (int i = 0; i < bunnyLair.GetLength(0); i++)
  26.         {
  27.             string currentLine = Console.ReadLine();
  28.  
  29.             for (int j = 0; j < bunnyLair.GetLength(1); j++)
  30.             {
  31.                 if (currentLine[j] == 'P')
  32.                 {
  33.                     rowOfPerson = i;
  34.                     colOfPerson = j;
  35.                 }
  36.  
  37.                 bunnyLair[i, j] = currentLine[j];
  38.             }
  39.         }
  40.  
  41.         string lineOfCommands = Console.ReadLine();
  42.         string situation = "";
  43.  
  44.         for (int i = 0; i < lineOfCommands.Length; i++)
  45.         {
  46.             situation = MovePerson(lineOfCommands[i], rowOfPerson, colOfPerson, situation);
  47.  
  48.             if (situation == "Won"
  49.                 || situation == "Lost")
  50.             {
  51.                 if (situation == "Lost")
  52.                 {
  53.                     rowOfPerson = UpdateRow(lineOfCommands[i], rowOfPerson);
  54.                     colOfPerson = UpdateCol(lineOfCommands[i], colOfPerson);
  55.                 }
  56.  
  57.                 situation = MultiplyBunnies(situation);
  58.                 break;
  59.             }
  60.             rowOfPerson = UpdateRow(lineOfCommands[i], rowOfPerson);
  61.             colOfPerson = UpdateCol(lineOfCommands[i], colOfPerson);
  62.  
  63.             situation = MultiplyBunnies(situation);
  64.             if (situation == "Lost")
  65.             {
  66.                 break;
  67.             }
  68.         }
  69.  
  70.         if (situation == "Won" ||
  71.             situation == "Neutral")
  72.         {
  73.             PrintLair(bunnyLair);
  74.             Console.WriteLine("won: {0} {1}", rowOfPerson, colOfPerson);
  75.         }
  76.  
  77.         else
  78.         {
  79.             PrintLair(bunnyLair);
  80.             Console.WriteLine("dead: {0} {1}", rowOfPerson, colOfPerson);
  81.         }
  82.     }
  83.  
  84.     private static string MovePerson(
  85.         char currentCommand, int rowOfPerson, int colOfPerson, string situation)
  86.     {
  87.         try
  88.         {
  89.             switch (currentCommand)
  90.             {
  91.                 case 'U':
  92.                     if (bunnyLair[rowOfPerson - 1, colOfPerson] == 'B')
  93.                     {
  94.                         return situation = "Lost";
  95.                     }
  96.                     bunnyLair[rowOfPerson - 1, colOfPerson] = 'P';
  97.                     bunnyLair[rowOfPerson, colOfPerson] = '.';
  98.                     break;
  99.                 case 'D':
  100.                     if (bunnyLair[rowOfPerson + 1, colOfPerson] == 'B')
  101.                     {
  102.                         return situation = "Lost";
  103.                     }
  104.                     bunnyLair[rowOfPerson + 1, colOfPerson] = 'P';
  105.                     bunnyLair[rowOfPerson, colOfPerson] = '.';
  106.                     break;
  107.                 case 'L':
  108.                     if (bunnyLair[rowOfPerson, colOfPerson - 1] == 'B')
  109.                     {
  110.                         return situation = "Lost";
  111.                     }
  112.                     bunnyLair[rowOfPerson, colOfPerson - 1] = 'P';
  113.                     bunnyLair[rowOfPerson, colOfPerson] = '.';
  114.                     break;
  115.                 case 'R':
  116.                     if (bunnyLair[rowOfPerson, colOfPerson + 1] == 'B')
  117.                     {
  118.                         return situation = "Lost";
  119.                     }
  120.                     bunnyLair[rowOfPerson, colOfPerson + 1] = 'P';
  121.                     bunnyLair[rowOfPerson, colOfPerson] = '.';
  122.                     break;
  123.  
  124.             }
  125.             return situation = "Neutral";
  126.         }
  127.         catch (Exception)
  128.         {
  129.             bunnyLair[rowOfPerson, colOfPerson] = '.';
  130.             return situation = "Won";
  131.         }
  132.     }
  133.  
  134.     private static string MultiplyBunnies(string situation)
  135.     {
  136.         int rowsLength = bunnyLair.GetLength(0);
  137.         int colsLength = bunnyLair.GetLength(1);
  138.  
  139.         bool[,] mask = new bool[rowsLength, colsLength];
  140.  
  141.         // Set new positions in mask
  142.         for (int i = 0; i < rowsLength; i++)
  143.         {
  144.             for (int j = 0; j < colsLength; j++)
  145.             {
  146.                 if (bunnyLair[i, j] == 'B')
  147.                 {
  148.                     situation = SpreadBunnie(i, j, mask, rowsLength, colsLength, situation);
  149.                 }
  150.             }
  151.         }
  152.  
  153.         // Apply mask
  154.         for (int i = 0; i < rowsLength; i++)
  155.         {
  156.             for (int j = 0; j < colsLength; j++)
  157.             {
  158.                 if (mask[i, j])
  159.                 {
  160.                     bunnyLair[i, j] = 'B';
  161.                 }
  162.             }
  163.         }
  164.  
  165.         if (situation == "Lost")
  166.         {
  167.             return situation;
  168.         }
  169.  
  170.         return "Neutral";
  171.     }
  172.  
  173.     private static string SpreadBunnie(
  174.         int i, int j, bool[,] mask, int rowsLength, int colsLength, string situation)
  175.     {
  176.         if (IsWithinField(i + 1, j, rowsLength, colsLength))
  177.         {
  178.             if (bunnyLair[i + 1, j] == 'P')
  179.             {
  180.                 situation = "Lost";
  181.             }
  182.             mask[i + 1, j] = true;
  183.         }
  184.  
  185.         if (IsWithinField(i - 1, j, rowsLength, colsLength))
  186.         {
  187.             if (bunnyLair[i - 1, j] == 'P')
  188.             {
  189.                 situation = "Lost";
  190.             }
  191.             mask[i - 1, j] = true;
  192.         }
  193.  
  194.         if (IsWithinField(i, j + 1, rowsLength, colsLength))
  195.         {
  196.             if (bunnyLair[i, j + 1] == 'P')
  197.             {
  198.  
  199.                 situation = "Lost";
  200.             }
  201.             mask[i, j + 1] = true;
  202.         }
  203.  
  204.         if (IsWithinField(i, j - 1, rowsLength, colsLength))
  205.         {
  206.             if (bunnyLair[i, j - 1] == 'P')
  207.             {
  208.                 situation = "Lost";
  209.             }
  210.  
  211.             mask[i, j - 1] = true;
  212.         }
  213.  
  214.         return situation;
  215.     }
  216.  
  217.     private static int UpdateRow(char currentCommand, int rowOfPerson)
  218.     {
  219.         switch (currentCommand)
  220.         {
  221.             case 'U':
  222.                 rowOfPerson -= 1;
  223.                 break;
  224.             case 'D':
  225.                 rowOfPerson += 1;
  226.                 break;
  227.         }
  228.         return rowOfPerson;
  229.     }
  230.  
  231.     private static int UpdateCol(char currentCommand, int colOfPerson)
  232.     {
  233.         switch (currentCommand)
  234.         {
  235.             case 'L':
  236.                 colOfPerson -= 1;
  237.                 break;
  238.             case 'R':
  239.                 colOfPerson += 1;
  240.                 break;
  241.         }
  242.         return colOfPerson;
  243.     }
  244.  
  245.     private static void PrintLair(char[,] bunnyLair)
  246.     {
  247.         for (int i = 0; i < bunnyLair.GetLength(0); i++)
  248.         {
  249.             for (int j = 0; j < bunnyLair.GetLength(1); j++)
  250.             {
  251.                 Console.Write(bunnyLair[i, j]);
  252.             }
  253.             Console.WriteLine();
  254.         }
  255.     }
  256.  
  257.     private static bool IsWithinField(int row, int col, int totalRows, int totalCols)
  258.     {
  259.         bool isWithin =
  260.             0 <= row && row < totalRows &&
  261.             0 <= col && col < totalCols;
  262.  
  263.         return isWithin;
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement