Advertisement
Guest User

Untitled

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