Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.06 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.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] dimensions = Console.ReadLine()
  13.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  14.                 .Select(int.Parse)
  15.                 .ToArray();
  16.  
  17.             int rows = dimensions[0];
  18.             int columns = dimensions[1];
  19.  
  20.             char[,] lair = new char[rows, columns];
  21.  
  22.             int currentRow = 0;
  23.             int currentColumn = 0;
  24.  
  25.             for (int row = 0; row < rows; row++)
  26.             {
  27.                 char[] symbols = Console.ReadLine().ToCharArray();
  28.  
  29.                 for (int col = 0; col < columns; col++)
  30.                 {
  31.                     if (symbols[col] == 'P')
  32.                     {
  33.                         currentRow = row;
  34.                         currentColumn = col;
  35.                     }
  36.                     lair[row, col] = symbols[col];
  37.                 }
  38.             }
  39.  
  40.             char[] directions = Console.ReadLine().ToCharArray();
  41.  
  42.             bool isDead = false;
  43.             bool wentOut = false;
  44.  
  45.             for (int i = 0; i < directions.Length; i++)
  46.             {
  47.                 char current = directions[i];
  48.  
  49.                 if (current == 'L')
  50.                 {
  51.                     if (IsInside(lair, currentRow, currentColumn - 1))
  52.                     {
  53.                         lair[currentRow, currentColumn] = '.';
  54.                         currentColumn -= 1;
  55.                     }
  56.                     else
  57.                     {
  58.                         wentOut = true;
  59.                         break;
  60.                     }
  61.                 }
  62.                 else if (current == 'R')
  63.                 {
  64.                     if (IsInside(lair, currentRow, currentColumn + 1))
  65.                     {
  66.                         lair[currentRow, currentColumn] = '.';
  67.                         currentColumn += 1;
  68.                     }
  69.                     else
  70.                     {
  71.                         wentOut = true;
  72.                         break;
  73.                     }
  74.                 }
  75.                 else if (current == 'U')
  76.                 {
  77.                     if (IsInside(lair, currentRow - 1, currentColumn))
  78.                     {
  79.                         lair[currentRow, currentColumn] = '.';
  80.                         currentRow -= 1;
  81.                     }
  82.                     else
  83.                     {
  84.                         wentOut = true;
  85.                         break;
  86.                     }
  87.                 }
  88.                 else if (current == 'D')
  89.                 {
  90.                     if (IsInside(lair, currentRow + 1, currentColumn))
  91.                     {
  92.                         lair[currentRow, currentColumn] = '.';
  93.                         currentRow += 1;
  94.                     }
  95.                     else
  96.                     {
  97.                         wentOut = true;
  98.                         break;
  99.                     }
  100.                 }
  101.  
  102.                 if (lair[currentRow, currentColumn] == 'B')
  103.                 {
  104.                     isDead = true;
  105.                     break;
  106.                 }
  107.  
  108.                 lair[currentRow, currentColumn] = 'P';
  109.  
  110.                 for (int row = 0; row < rows; row++)
  111.                 {
  112.                     for (int col = 0; col < columns; col++)
  113.                     {
  114.                         if (lair[row, col] == 'B')
  115.                         {
  116.                             if (IsInside(lair, row - 1, col))
  117.                             {
  118.                                 if (lair[row - 1, col] == 'P')
  119.                                 {
  120.                                     isDead = true;
  121.                                     break;
  122.                                 }
  123.  
  124.                                 lair[row - 1, col] = 'B';
  125.                             }
  126.  
  127.                             if (IsInside(lair, row + 1, col))
  128.                             {
  129.                                 if (lair[row + 1, col] == 'P')
  130.                                 {
  131.                                     isDead = true;
  132.                                     break;
  133.                                 }
  134.  
  135.                                 lair[row + 1, col] = 'B';
  136.                             }
  137.  
  138.                             if (IsInside(lair, row, col - 1))
  139.                             {
  140.                                 if (lair[row, col - 1] == 'P')
  141.                                 {
  142.                                     isDead = true;
  143.                                     break;
  144.                                 }
  145.  
  146.                                 lair[row, col - 1] = 'B';
  147.                             }
  148.  
  149.                             if (IsInside(lair, row, col + 1))
  150.                             {
  151.                                 if (lair[row, col + 1] == 'P')
  152.                                 {
  153.                                     isDead = true;
  154.                                     break;
  155.                                 }
  156.  
  157.                                 lair[row, col + 1] = 'B';
  158.                             }
  159.                         }
  160.                     }
  161.                 }
  162.             }
  163.  
  164.             for (int row = 0; row < lair.GetLength(0); row++)
  165.             {
  166.                 for (int col = 0; col < lair.GetLength(1); col++)
  167.                 {
  168.                     Console.Write($"{lair[row, col]}");
  169.                 }
  170.  
  171.                 Console.WriteLine();
  172.             }
  173.  
  174.             if (isDead)
  175.             {
  176.                 Console.WriteLine($"dead: {currentRow} {currentColumn}");
  177.             }
  178.            
  179.             if (wentOut && !isDead)
  180.             {
  181.                 Console.WriteLine($"won: {currentRow} {currentColumn}");
  182.             }
  183.         }
  184.  
  185.         private static bool IsInside(char[,] matrix, int row, int col)
  186.         {
  187.             return row >= 0 && row < matrix.GetLength(0)
  188.                             && col >= 0 && col < matrix.GetLength(1);
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement