Advertisement
Guest User

Untitled

a guest
May 24th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.05 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel.Design;
  3. using System.Linq;
  4. using System.Numerics;
  5.  
  6. namespace Problem10RadioactiveMutantVampireBunnies
  7. {
  8.     public static class DebugExtensions
  9.     {
  10.         public static string Test2D(this Array source, int pad = 10)
  11.         {
  12.             var result = "";
  13.             for (int i = source.GetLowerBound(0); i <= source.GetUpperBound(0); i++)
  14.             {
  15.                 for (int j = source.GetLowerBound(1); j <= source.GetUpperBound(1); j++)
  16.                     result += source.GetValue(i, j).ToString().PadLeft(pad);
  17.                 result += "\n";
  18.             }
  19.             return result;
  20.         }
  21.     }
  22.  
  23.     class Program
  24.     {
  25.         static void Main(string[] args)
  26.         {
  27.             int[] dimensions = Console.ReadLine()
  28.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  29.                 .Select(int.Parse)
  30.                 .ToArray();
  31.  
  32.             int rows = dimensions[0];
  33.             int columns = dimensions[1];
  34.  
  35.             char[,] lair = new char[rows, columns];
  36.  
  37.             int currentRow = 0;
  38.             int currentColumn = 0;
  39.  
  40.             for (int row = 0; row < rows; row++)
  41.             {
  42.                 char[] symbols = Console.ReadLine().ToCharArray();
  43.  
  44.                 for (int col = 0; col < columns; col++)
  45.                 {
  46.                     if (symbols[col] == 'P')
  47.                     {
  48.                         currentRow = row;
  49.                         currentColumn = col;
  50.                     }
  51.  
  52.                     lair[row, col] = symbols[col];
  53.                 }
  54.             }
  55.  
  56.             char[] directions = Console.ReadLine().ToCharArray();
  57.  
  58.             bool isDead = false;
  59.             bool wentOut = false;
  60.  
  61.             for (int i = 0; i < directions.Length; i++)
  62.             {
  63.                 char current = directions[i];
  64.  
  65.                 if (!isDead && !wentOut)
  66.                 {
  67.                     if (current == 'L')
  68.                     {
  69.                         if (IsInside(lair, currentRow, currentColumn - 1))
  70.                         {
  71.                             if (lair[currentRow, currentColumn - 1] == 'B')
  72.                             {
  73.                                 isDead = true;
  74.                                 lair[currentRow, currentColumn] = '.';
  75.                             }
  76.                             else
  77.                             {
  78.                                 lair[currentRow, currentColumn] = '.';
  79.                                 currentColumn -= 1;
  80.                                 lair[currentRow, currentColumn] = 'P';
  81.                             }
  82.                         }
  83.                         else
  84.                         {
  85.                             wentOut = true;
  86.                             lair[currentRow, currentColumn] = '.';
  87.                         }
  88.                     }
  89.                     else if (current == 'R')
  90.                     {
  91.                         if (IsInside(lair, currentRow, currentColumn + 1))
  92.                         {
  93.                             if (lair[currentRow, currentColumn + 1] == 'B')
  94.                             {
  95.                                 isDead = true;
  96.                                 lair[currentRow, currentColumn] = '.';
  97.                             }
  98.                             else
  99.                             {
  100.                                 lair[currentRow, currentColumn] = '.';
  101.                                 currentColumn += 1;
  102.                                 lair[currentRow, currentColumn] = 'P';
  103.                             }
  104.                         }
  105.                         else
  106.                         {
  107.                             wentOut = true;
  108.                             lair[currentRow, currentColumn] = '.';
  109.                         }
  110.                     }
  111.                     else if (current == 'U')
  112.                     {
  113.                         if (IsInside(lair, currentRow - 1, currentColumn))
  114.                         {
  115.                             if (lair[currentRow - 1, currentColumn] == 'B')
  116.                             {
  117.                                 isDead = true;
  118.                                 lair[currentRow, currentColumn] = '.';
  119.                             }
  120.                             else
  121.                             {
  122.                                 lair[currentRow, currentColumn] = '.';
  123.                                 currentRow -= 1;
  124.                                 lair[currentRow, currentColumn] = 'P';
  125.                             }
  126.                         }
  127.                         else
  128.                         {
  129.                             wentOut = true;
  130.                             lair[currentRow, currentColumn] = '.';
  131.                         }
  132.                     }
  133.                     else if (current == 'D')
  134.                     {
  135.                         if (IsInside(lair, currentRow + 1, currentColumn))
  136.                         {
  137.                             if (lair[currentRow + 1, currentColumn] == 'B')
  138.                             {
  139.                                 isDead = true;
  140.                                 lair[currentRow, currentColumn] = '.';
  141.                             }
  142.                             else
  143.                             {
  144.                                 lair[currentRow, currentColumn] = '.';
  145.                                 currentRow += 1;
  146.                                 lair[currentRow, currentColumn] = 'P';
  147.                             }
  148.                         }
  149.                         else
  150.                         {
  151.                             wentOut = true;
  152.                             lair[currentRow, currentColumn] = '.';
  153.                         }
  154.                     }
  155.  
  156.                     for (int row = 0; row < rows; row++)
  157.                     {
  158.                         for (int col = 0; col < columns; col++)
  159.                         {
  160.                             if (lair[row, col] == 'B')
  161.                             {
  162.                                 if (IsInside(lair, row, col - 1))
  163.                                 {
  164.                                     if (lair[row, col - 1] == 'P')
  165.                                     {
  166.                                         isDead = true;
  167.                                         lair[row, col - 1] = '.';
  168.                                     }
  169.  
  170.                                     lair[row, col - 1] = 'C';
  171.                                 }
  172.  
  173.                                 if (IsInside(lair, row, col + 1))
  174.                                 {
  175.                                     if (lair[row, col + 1] == 'P')
  176.                                     {
  177.                                         isDead = true;
  178.                                         lair[row, col + 1] = '.';
  179.                                     }
  180.  
  181.                                     lair[row, col + 1] = 'C';
  182.                                 }
  183.  
  184.                                 if (IsInside(lair, row - 1, col))
  185.                                 {
  186.                                     if (lair[row - 1, col] == 'P')
  187.                                     {
  188.                                         isDead = true;
  189.                                         lair[row - 1, col] = '.';
  190.                                     }
  191.  
  192.                                     lair[row - 1, col] = 'C';
  193.                                 }
  194.  
  195.                                 if (IsInside(lair, row + 1, col))
  196.                                 {
  197.                                     if (lair[row + 1, col] == 'P')
  198.                                     {
  199.                                         isDead = true;
  200.                                         lair[row + 1, col] = '.';
  201.                                     }
  202.  
  203.                                     lair[row + 1, col] = 'C';
  204.                                 }
  205.                             }
  206.                         }
  207.                     }
  208.  
  209.  
  210.                     for (int row = 0; row < rows; row++)
  211.                     {
  212.                         for (int col = 0; col < columns; col++)
  213.                         {
  214.                             if (lair[row, col] == 'C')
  215.                             {
  216.                                 lair[row, col] = 'B';
  217.                             }
  218.                         }
  219.                     }
  220.                 }
  221.             }
  222.  
  223.             for (int row = 0; row < rows; row++)
  224.             {
  225.                 for (int col = 0; col < columns; col++)
  226.                 {
  227.                     Console.Write($"{lair[row, col]}");
  228.                 }
  229.                 Console.WriteLine();
  230.             }
  231.  
  232.             if (isDead)
  233.             {
  234.                 Console.WriteLine($"dead: {currentRow} {currentColumn}");
  235.             }
  236.             else
  237.             {
  238.                 Console.WriteLine($"won: {currentRow} {currentColumn}");
  239.             }
  240.         }
  241.  
  242.         private static bool IsInside(char[,] matrix, int row, int col)
  243.         {
  244.             return row >= 0 && row < matrix.GetLength(0)
  245.                             && col >= 0 && col < matrix.GetLength(1);
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement