Advertisement
kaloyan_kolev

Untitled

May 24th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.73 KB | None | 0 0
  1. sing 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.     class Program
  23.     {
  24.         static void Main(string[] args)
  25.         {
  26.             int[] dimensions = Console.ReadLine()
  27.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  28.                 .Select(int.Parse)
  29.                 .ToArray();
  30.  
  31.             int rows = dimensions[0];
  32.             int columns = dimensions[1];
  33.  
  34.             char[,] lair = new char[rows, columns];
  35.  
  36.             int currentRow = 0;
  37.             int currentColumn = 0;
  38.  
  39.             for (int row = 0; row < rows; row++)
  40.             {
  41.                 char[] symbols = Console.ReadLine().ToCharArray();
  42.  
  43.                 for (int col = 0; col < columns; col++)
  44.                 {
  45.                     if (symbols[col] == 'P')
  46.                     {
  47.                         currentRow = row;
  48.                         currentColumn = col;
  49.                     }
  50.  
  51.                     lair[row, col] = symbols[col];
  52.                 }
  53.             }
  54.  
  55.             char[] directions = Console.ReadLine().ToCharArray();
  56.  
  57.             bool isDead = false;
  58.             bool wentOut = false;
  59.  
  60.             for (int i = 0; i < directions.Length; i++)
  61.             {
  62.                 char currentDirection = directions[i];
  63.  
  64.                 if (!isDead && !wentOut)
  65.                 {
  66.                     if (currentDirection == 'L')
  67.                     {
  68.                         if (IsInside(lair, currentRow, currentColumn - 1))
  69.                         {
  70.                             lair[currentRow, currentColumn] = '.';
  71.  
  72.                             if (lair[currentRow, currentColumn - 1] == 'B')
  73.                             {
  74.                                 isDead = true;
  75.                                 currentColumn -= 1;
  76.                             }
  77.                             else
  78.                             {
  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 (currentDirection == 'R')
  90.                     {
  91.                         if (IsInside(lair, currentRow, currentColumn + 1))
  92.                         {
  93.                             lair[currentRow, currentColumn] = '.';
  94.                             if (lair[currentRow, currentColumn + 1] == 'B')
  95.                             {
  96.                                 isDead = true;
  97.                                 currentColumn += 1;
  98.                             }
  99.                             else
  100.                             {
  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 (currentDirection == 'U')
  112.                     {
  113.                         if (IsInside(lair, currentRow - 1, currentColumn))
  114.                         {
  115.                             lair[currentRow, currentColumn] = '.';
  116.                             if (lair[currentRow - 1, currentColumn] == 'B')
  117.                             {
  118.                                 isDead = true;
  119.                                 currentRow -= 1;
  120.                             }
  121.                             else
  122.                             {
  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 (currentDirection == 'D')
  134.                     {
  135.                         if (IsInside(lair, currentRow + 1, currentColumn))
  136.                         {
  137.                             lair[currentRow, currentColumn] = '.';
  138.                             if (lair[currentRow + 1, currentColumn] == 'B')
  139.                             {
  140.                                 isDead = true;
  141.                                 currentRow += 1;
  142.                             }
  143.                             else
  144.                             {
  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.                                     }
  168.  
  169.                                     lair[row, col - 1] = 'C';
  170.                                 }
  171.  
  172.                                 if (IsInside(lair, row, col + 1))
  173.                                 {
  174.                                     if (lair[row, col + 1] == 'P')
  175.                                     {
  176.                                         isDead = true;
  177.                                     }
  178.  
  179.                                     lair[row, col + 1] = 'C';
  180.                                 }
  181.  
  182.                                 if (IsInside(lair, row - 1, col))
  183.                                 {
  184.                                     if (lair[row - 1, col] == 'P')
  185.                                     {
  186.                                         isDead = true;
  187.                                     }
  188.  
  189.                                     lair[row - 1, col] = 'C';
  190.                                 }
  191.  
  192.                                 if (IsInside(lair, row + 1, col))
  193.                                 {
  194.                                     if (lair[row + 1, col] == 'P')
  195.                                     {
  196.                                         isDead = true;
  197.                                     }
  198.  
  199.                                     lair[row + 1, col] = 'C';
  200.                                 }
  201.                             }
  202.                         }
  203.                     }
  204.  
  205.  
  206.                     for (int row = 0; row < rows; row++)
  207.                     {
  208.                         for (int col = 0; col < columns; col++)
  209.                         {
  210.                             if (lair[row, col] == 'C')
  211.                             {
  212.                                 lair[row, col] = 'B';
  213.                             }
  214.                         }
  215.                     }
  216.                 }
  217.             }
  218.  
  219.             for (int row = 0; row < rows; row++)
  220.             {
  221.                 for (int col = 0; col < columns; col++)
  222.                 {
  223.                     Console.Write($"{lair[row, col]}");
  224.                 }
  225.                 Console.WriteLine();
  226.             }
  227.  
  228.             if (isDead)
  229.             {
  230.                 Console.WriteLine($"dead: {currentRow} {currentColumn}");
  231.             }
  232.             else
  233.             {
  234.                 Console.WriteLine($"won: {currentRow} {currentColumn}");
  235.             }
  236.         }
  237.  
  238.         private static bool IsInside(char[,] matrix, int row, int col)
  239.         {
  240.             return row >= 0 && row < matrix.GetLength(0)
  241.                             && col >= 0 && col < matrix.GetLength(1);
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement