Advertisement
Guest User

Untitled

a guest
May 24th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.01 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.             int deadRow = 0;
  61.             int deadCol = 0;
  62.  
  63.             for (int i = 0; i < directions.Length; i++)
  64.             {
  65.                 char current = directions[i];
  66.  
  67.                 if (!isDead && !wentOut)
  68.                 {
  69.                     if (current == 'L')
  70.                     {
  71.                         if (IsInside(lair, currentRow, currentColumn - 1))
  72.                         {
  73.                             if (lair[currentRow, currentColumn - 1] == 'B')
  74.                             {
  75.                                 isDead = true;
  76.                                 deadRow = currentRow;
  77.                                 deadCol = currentColumn - 1;
  78.                                 lair[currentRow, currentColumn] = '.';
  79.                             }
  80.                             else
  81.                             {
  82.                                 lair[currentRow, currentColumn] = '.';
  83.                                 currentColumn -= 1;
  84.                                 lair[currentRow, currentColumn] = 'P';
  85.                             }
  86.                         }
  87.                         else
  88.                         {
  89.                             wentOut = true;
  90.                             lair[currentRow, currentColumn] = '.';
  91.                         }
  92.                     }
  93.                     else if (current == 'R')
  94.                     {
  95.                         if (IsInside(lair, currentRow, currentColumn + 1))
  96.                         {
  97.                             if (lair[currentRow, currentColumn + 1] == 'B')
  98.                             {
  99.                                 isDead = true;
  100.                                 deadRow = currentRow;
  101.                                 deadCol = currentColumn + 1;
  102.                                 lair[currentRow, currentColumn] = '.';
  103.                             }
  104.                             else
  105.                             {
  106.                                 lair[currentRow, currentColumn] = '.';
  107.                                 currentColumn += 1;
  108.                                 lair[currentRow, currentColumn] = 'P';
  109.                             }
  110.                         }
  111.                         else
  112.                         {
  113.                             wentOut = true;
  114.                             lair[currentRow, currentColumn] = '.';
  115.                         }
  116.                     }
  117.                     else if (current == 'U')
  118.                     {
  119.                         if (IsInside(lair, currentRow - 1, currentColumn))
  120.                         {
  121.                             if (lair[currentRow - 1, currentColumn] == 'B')
  122.                             {
  123.                                 isDead = true;
  124.                                 deadRow = currentRow - 1;
  125.                                 deadCol = currentColumn;
  126.                                 lair[currentRow, currentColumn] = '.';
  127.                             }
  128.                             else
  129.                             {
  130.                                 lair[currentRow, currentColumn] = '.';
  131.                                 currentRow -= 1;
  132.                                 lair[currentRow, currentColumn] = 'P';
  133.                             }
  134.                         }
  135.                         else
  136.                         {
  137.                             wentOut = true;
  138.                             lair[currentRow, currentColumn] = '.';
  139.                         }
  140.                     }
  141.                     else if (current == 'D')
  142.                     {
  143.                         if (IsInside(lair, currentRow + 1, currentColumn))
  144.                         {
  145.                             if (lair[currentRow + 1, currentColumn] == 'B')
  146.                             {
  147.                                 isDead = true;
  148.                                 deadRow = currentRow + 1;
  149.                                 deadCol = currentColumn;
  150.                                 lair[currentRow, currentColumn] = '.';
  151.                             }
  152.                             else
  153.                             {
  154.                                 lair[currentRow, currentColumn] = '.';
  155.                                 currentRow += 1;
  156.                                 lair[currentRow, currentColumn] = 'P';
  157.                             }
  158.                         }
  159.                         else
  160.                         {
  161.                             wentOut = true;
  162.                             lair[currentRow, currentColumn] = '.';
  163.                         }
  164.                     }
  165.  
  166.                     for (int row = 0; row < rows; row++)
  167.                     {
  168.                         for (int col = 0; col < columns; col++)
  169.                         {
  170.                             if (lair[row, col] == 'B')
  171.                             {
  172.                                 if (IsInside(lair, row, col - 1))
  173.                                 {
  174.                                     if (lair[row, col - 1] == 'P')
  175.                                     {
  176.                                         isDead = true;
  177.                                         deadRow = row;
  178.                                         deadCol = col - 1;
  179.                                         lair[row, col - 1] = '.';
  180.                                     }
  181.  
  182.                                     lair[row, col - 1] = 'C';
  183.                                 }
  184.  
  185.                                 if (IsInside(lair, row, col + 1))
  186.                                 {
  187.                                     if (lair[row, col + 1] == 'P')
  188.                                     {
  189.                                         isDead = true;
  190.                                         deadRow = row;
  191.                                         deadCol = col + 1;
  192.                                         lair[row, col + 1] = '.';
  193.                                     }
  194.  
  195.                                     lair[row, col + 1] = 'C';
  196.                                 }
  197.  
  198.                                 if (IsInside(lair, row - 1, col))
  199.                                 {
  200.                                     if (lair[row - 1, col] == 'P')
  201.                                     {
  202.                                         isDead = true;
  203.                                         deadRow = row - 1;
  204.                                         deadCol = col;
  205.                                         lair[row - 1, col] = '.';
  206.                                     }
  207.  
  208.                                     lair[row - 1, col] = 'C';
  209.                                 }
  210.  
  211.                                 if (IsInside(lair, row + 1, col))
  212.                                 {
  213.                                     if (lair[row + 1, col] == 'P')
  214.                                     {
  215.                                         isDead = true;
  216.                                         deadRow = row + 1;
  217.                                         deadCol = col;
  218.                                         lair[row + 1, col] = '.';
  219.                                     }
  220.  
  221.                                     lair[row + 1, col] = 'C';
  222.                                 }
  223.                             }
  224.                         }
  225.                     }
  226.  
  227.  
  228.                     for (int row = 0; row < rows; row++)
  229.                     {
  230.                         for (int col = 0; col < columns; col++)
  231.                         {
  232.                             if (lair[row, col] == 'C')
  233.                             {
  234.                                 lair[row, col] = 'B';
  235.                             }
  236.                         }
  237.                     }
  238.                 }
  239.             }
  240.  
  241.             for (int row = 0; row < rows; row++)
  242.             {
  243.                 for (int col = 0; col < columns; col++)
  244.                 {
  245.                     Console.Write($"{lair[row, col]}");
  246.                 }
  247.                 Console.WriteLine();
  248.             }
  249.  
  250.             if (isDead)
  251.             {
  252.                 Console.WriteLine($"dead: {deadRow} {deadCol}");
  253.             }
  254.             else
  255.             {
  256.                 Console.WriteLine($"won: {currentRow} {currentColumn}");
  257.             }
  258.         }
  259.  
  260.         private static bool IsInside(char[,] matrix, int row, int col)
  261.         {
  262.             return row >= 0 && row < matrix.GetLength(0)
  263.                             && col >= 0 && col < matrix.GetLength(1);
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement