Advertisement
EvgeniVT

Radioactive Mutant Vampire Bunnies

Sep 27th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Radioactive_Mutant_Vampire_Bunnies
  6. {
  7.     class Program
  8.     {
  9.         static int rows;
  10.         static int cols;
  11.         static List<int> list = new List<int>();
  12.         static char[][] matrix;
  13.         static int pRow;
  14.         static int pCol;
  15.         static bool live = true;
  16.         static bool win = false;
  17.         static void Main()
  18.         {
  19.             var nxm = Console.ReadLine()
  20.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  21.                 .Select(int.Parse)
  22.                 .ToArray();
  23.             rows = nxm[0];
  24.             cols = nxm[1];          
  25.             matrix = new char[rows][];
  26.             for (int row = 0; row < rows; row++)
  27.             {
  28.                 matrix[row] = new char[cols];
  29.                 var input = Console.ReadLine()                  
  30.                     .ToCharArray();        
  31.                 for (int col = 0; col < cols; col++)
  32.                 {
  33.                     matrix[row][col] = input[col];
  34.                     if (matrix[row][col] == 'P')
  35.                     {
  36.                         pRow = row;
  37.                         pCol = col;
  38.                     }
  39.                     else if (matrix[row][col] == 'B')
  40.                     {
  41.                         list.Add(row);
  42.                         list.Add(col);
  43.                     }
  44.                 }
  45.             }
  46.             var comm= Console.ReadLine();
  47.             foreach (var cha in comm)
  48.             {                          
  49.                 Move(cha);
  50.                 Spare();                        
  51.                 if (win)
  52.                 {
  53.                     Print();
  54.                     Console.WriteLine($"won: {pRow} {pCol}");
  55.                     break;
  56.                 }
  57.                 if (!live)
  58.                 {
  59.                     Print();
  60.                     Console.WriteLine($"dead: {pRow} {pCol}");
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.  
  66.         private static void Spare()
  67.         {
  68.             var n = list.Count;
  69.             for (int i = 0; i < n; i += 2)
  70.             {
  71.                 for (int row = -1; row < 2; row++)
  72.                 {
  73.                     for (int col = -1; col < 2; col++)
  74.                     {
  75.                         var r = list[i];
  76.                         var c = list[i + 1];
  77.                         if (!((row ==  0 && col ==  0) ||
  78.                               (row == -1 && col == -1) ||
  79.                               (row == -1 && col ==  1) ||
  80.                               (row ==  1 && col ==  1) ||
  81.                               (row ==  1 && col == -1)))
  82.                         {
  83.                             if (IsIn(r + row, c + col))
  84.                             {
  85.                                 if (matrix[r + row][c + col] == 'P') live = false;
  86.                                 matrix[r + row][c + col] = 'B';
  87.                                 list.Add(r + row);
  88.                                 list.Add(c + col);
  89.                             }
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.  
  96.         private static void Move(char ch)
  97.         {
  98.             switch (ch)
  99.             {
  100.                 case 'U': Direkt(pRow - 1, pCol - 0); break;
  101.                 case 'D': Direkt(pRow + 1, pCol - 0); break;
  102.                 case 'R': Direkt(pRow - 0, pCol + 1); break;
  103.                 case 'L': Direkt(pRow - 0, pCol - 1); break;
  104.             }          
  105.         }
  106.  
  107.         private static void Direkt(int r, int c)
  108.         {
  109.             matrix[pRow][pCol] = '.';
  110.             if (IsIn(r, c))
  111.             {
  112.                 if (matrix[r][c] == 'B')
  113.                     live = false;
  114.                 else
  115.                     matrix[r][c] = 'P';
  116.                 pRow = r;
  117.                 pCol = c;
  118.             }
  119.             else
  120.                 win = true;                    
  121.         }
  122.  
  123.         static bool IsIn(int r, int c)
  124.         {
  125.             if (r >= 0 && r < rows && c >= 0 && c < cols)
  126.                 return true;
  127.             else
  128.                 return false;
  129.         }
  130.         private static void Print()
  131.         {
  132.             foreach (var row in matrix)
  133.             {
  134.                 if (row.Any())
  135.                     Console.WriteLine(string.Join("", row));
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement