Advertisement
elena1234

RadioactiveMutantVampireBunnies- multidimensional array

Dec 28th, 2020 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.81 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace RadioactiveMutantVampireBunnies
  6. {
  7.     static class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
  12.             int rows = input[0];
  13.             int cols = input[1];
  14.             var matrix = new char[rows, cols];
  15.             int rowStart = 0;
  16.             int colStart = 0;
  17.             for (int row = 0; row < rows; row++)
  18.             {
  19.                 string currentLine = Console.ReadLine();
  20.                 for (int col = 0; col < cols; col++)
  21.                 {
  22.                     matrix[row, col] = currentLine[col];
  23.                     if (matrix[row, col] == 'P')
  24.                     {
  25.                         rowStart = row;
  26.                         colStart = col;
  27.                     }
  28.                 }
  29.             }
  30.  
  31.             string commandLine = Console.ReadLine();
  32.             foreach (var command in commandLine)
  33.             {
  34.                 int currentRow = rowStart;
  35.                 int currentCol = colStart;
  36.                 switch (command)
  37.                 {
  38.                     case 'U':
  39.                         currentRow--;
  40.                         break;
  41.                     case 'D':
  42.                         currentRow++;
  43.                         break;
  44.                     case 'L':
  45.                         currentCol--;
  46.                         break;
  47.                     case 'R':
  48.                         currentCol++;
  49.                         break;
  50.                 }
  51.  
  52.                 if (currentRow >= 0 && currentRow < rows && currentCol >= 0 && currentCol < cols) // the player is in the lair
  53.                 {
  54.                     if (matrix[currentRow, currentCol] != 'B') // The player doesn't reach bunny
  55.                     {
  56.                         matrix[rowStart, colStart] = '.';
  57.                         matrix[currentRow, currentCol] = 'P';
  58.                         var queueWithBunnies = new Queue<List<int>>();
  59.                         FindWhereAreTheBunnies(rows, cols, matrix, queueWithBunnies);
  60.                         BunniesMultiply(rows, cols, matrix, queueWithBunnies);
  61.                         if (matrix[currentRow, currentCol] != 'B') // after the multiply bunny doesn't reach the player
  62.                         {
  63.                             rowStart = currentRow;
  64.                             colStart = currentCol;
  65.                             continue;
  66.                         }
  67.  
  68.                         else if (matrix[currentRow, currentCol] == 'B') // after the multiply bunny reach the player
  69.                         {
  70.                             PrintTheMatrix(rows, cols, matrix);
  71.                             Console.WriteLine($"dead: {currentRow} {currentCol}");
  72.                             return;
  73.                         }
  74.                     }
  75.  
  76.                     else if (matrix[currentRow, currentCol] == 'B') // The player reaches bunny
  77.                     {
  78.                         var queueWithBunnies = new Queue<List<int>>();
  79.                         FindWhereAreTheBunnies(rows, cols, matrix, queueWithBunnies);
  80.                         BunniesMultiply(rows, cols, matrix, queueWithBunnies);
  81.                         PrintTheMatrix(rows, cols, matrix);
  82.                         Console.WriteLine($"dead: {currentRow} {currentCol}");
  83.                         return;
  84.                     }
  85.                 }
  86.  
  87.                 else // The player escapes from the lair    
  88.                 {
  89.                     matrix[rowStart, colStart] = '.';
  90.                     var queueWithBunnies = new Queue<List<int>>();
  91.                     FindWhereAreTheBunnies(rows, cols, matrix, queueWithBunnies);
  92.                     BunniesMultiply(rows, cols, matrix, queueWithBunnies);
  93.                     PrintTheMatrix(rows, cols, matrix);
  94.                     Console.WriteLine($"won: {rowStart} {colStart}");
  95.                     return;
  96.                 }
  97.             }
  98.         }
  99.  
  100.  
  101.         private static void BunniesMultiply(int rows, int cols, char[,] matrix, Queue<List<int>> queueWithBunnies)
  102.         {
  103.             while (queueWithBunnies.Count > 0)
  104.             {
  105.                 var bunniesCoordinates = queueWithBunnies.Dequeue();
  106.                 int rowBunnies = bunniesCoordinates[0];
  107.                 int colBunnies = bunniesCoordinates[1];
  108.                 if (rowBunnies - 1 >= 0)
  109.                 {
  110.                     matrix[rowBunnies - 1, colBunnies] = 'B';
  111.                 }
  112.  
  113.                 if (rowBunnies + 1 < rows)
  114.                 {
  115.                     matrix[rowBunnies + 1, colBunnies] = 'B';
  116.                 }
  117.  
  118.                 if (colBunnies - 1 >= 0)
  119.                 {
  120.                     matrix[rowBunnies, colBunnies - 1] = 'B';
  121.                 }
  122.  
  123.                 if (colBunnies + 1 < cols)
  124.                 {
  125.                     matrix[rowBunnies, colBunnies + 1] = 'B';
  126.                 }
  127.             }
  128.         }
  129.  
  130.         private static void FindWhereAreTheBunnies(int rows, int cols, char[,] matrix, Queue<List<int>> queueWithBunnies)
  131.         {
  132.             for (int row = 0; row < rows; row++)
  133.             {
  134.                 for (int col = 0; col < cols; col++)
  135.                 {
  136.                     if (matrix[row, col] == 'B')
  137.                     {
  138.                         queueWithBunnies.Enqueue(new List<int> { row, col });
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.  
  144.         private static void PrintTheMatrix(int rows, int cols, char[,] matrix)
  145.         {
  146.             for (int row = 0; row < rows; row++)
  147.             {
  148.                 for (int col = 0; col < cols; col++)
  149.                 {
  150.                     Console.Write(matrix[row, col]);
  151.                 }
  152.  
  153.                 Console.WriteLine();
  154.             }
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement