Advertisement
yanchevilian

8. *Bombs

Aug 30th, 2021
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _8._Bombs
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int dimensions = int.Parse(Console.ReadLine());
  12.             int[,] squareMatrix = FillSquareMatrix(dimensions);
  13.  
  14.             int[] coordinatesOfBomb = Console.ReadLine()
  15.                                              .Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries)
  16.                                              .Select(int.Parse)
  17.                                              .ToArray();
  18.             for (int i = 0; i < coordinatesOfBomb.Length; i+=2)
  19.             {
  20.                 int currentRowBomb = coordinatesOfBomb[i];
  21.                  int currentColBomb = coordinatesOfBomb[i + 1];
  22.  
  23.                 if (IsValid(currentRowBomb, currentColBomb, squareMatrix) &&
  24.                     squareMatrix[currentRowBomb, currentColBomb] > 0)
  25.                 {
  26.                     int bombDamage = squareMatrix[currentRowBomb, currentColBomb];
  27.                     squareMatrix = Explosion(bombDamage, currentRowBomb, currentColBomb, squareMatrix);
  28.                 }
  29.             }
  30.  
  31.             List<int> activeCells = FindActiveCells(squareMatrix);
  32.             Console.WriteLine($"Alive cells: {activeCells.Count}");
  33.             Console.WriteLine($"Sum: {activeCells.Sum()}");
  34.             PrintMatrix(squareMatrix);
  35.  
  36.         }
  37.  
  38.         private static void PrintMatrix(int[,] squareMatrix)
  39.         {
  40.             for (int row = 0; row < squareMatrix.GetLength(0); row++)
  41.             {
  42.                 for (int col = 0; col < squareMatrix.GetLength(1); col++)
  43.                 {
  44.                     Console.Write(squareMatrix[row, col] + " ");
  45.                 }
  46.  
  47.                 Console.WriteLine();
  48.             }
  49.         }
  50.  
  51.         public static List<int> FindActiveCells(int[,] squareMatrix)
  52.         {
  53.             List<int> activeCells = new List<int>();
  54.  
  55.             for (int row = 0; row < squareMatrix.GetLength(0); row++)
  56.             {
  57.                 for (int col = 0; col < squareMatrix.GetLength(1); col++)
  58.                 {
  59.                     if (squareMatrix[row, col] > 0)
  60.                     {
  61.                         activeCells.Add(squareMatrix[row,col]);
  62.                     }  
  63.                 }
  64.             }
  65.  
  66.             return activeCells;
  67.         }
  68.  
  69.         public static int[,] FillSquareMatrix(int dimensions)
  70.         {
  71.             int[,] squareMatrix = new int[dimensions, dimensions];
  72.  
  73.             for (int row = 0; row < squareMatrix.GetLength(0); row++)
  74.             {
  75.                 int[] numbersToFill = Console.ReadLine()
  76.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  77.                     .Select(int.Parse)
  78.                     .ToArray();
  79.                 for (int col = 0; col < squareMatrix.GetLength(1); col++)
  80.                 {
  81.                     squareMatrix[row, col] = numbersToFill[col];
  82.                 }
  83.             }
  84.  
  85.             return squareMatrix;
  86.         }
  87.  
  88.         private static int[,] Explosion(int bombDamage, int currentRowBomb, int currentColBomb, int[,] squareMatrix)
  89.         {
  90.             if (IsValid(currentRowBomb - 1, currentColBomb, squareMatrix) && squareMatrix[currentRowBomb - 1, currentColBomb] > 0)
  91.             {
  92.                 squareMatrix[currentRowBomb - 1, currentColBomb] -= bombDamage;
  93.             }
  94.             if (IsValid(currentRowBomb + 1, currentColBomb, squareMatrix) && squareMatrix[currentRowBomb + 1, currentColBomb] > 0)
  95.             {
  96.                 squareMatrix[currentRowBomb + 1, currentColBomb] -= bombDamage;
  97.             }
  98.             if (IsValid(currentRowBomb - 1, currentColBomb - 1, squareMatrix) && squareMatrix[currentRowBomb - 1, currentColBomb - 1] > 0)
  99.             {
  100.                 squareMatrix[currentRowBomb - 1, currentColBomb - 1] -= bombDamage;
  101.             }
  102.             if (IsValid(currentRowBomb - 1, currentColBomb + 1, squareMatrix) && squareMatrix[currentRowBomb - 1, currentColBomb + 1] > 0)
  103.             {
  104.                 squareMatrix[currentRowBomb - 1, currentColBomb + 1] -= bombDamage;
  105.             }
  106.             if (IsValid(currentRowBomb + 1, currentColBomb - 1, squareMatrix) && squareMatrix[currentRowBomb + 1, currentColBomb - 1] > 0)
  107.             {
  108.                 squareMatrix[currentRowBomb + 1, currentColBomb - 1] -= bombDamage;
  109.             }
  110.             if (IsValid(currentRowBomb + 1, currentColBomb + 1, squareMatrix) && squareMatrix[currentRowBomb + 1, currentColBomb + 1] > 0)
  111.             {
  112.                 squareMatrix[currentRowBomb + 1, currentColBomb + 1] -= bombDamage;
  113.             }
  114.             if (IsValid(currentRowBomb, currentColBomb - 1, squareMatrix) && squareMatrix[currentRowBomb, currentColBomb - 1] > 0)
  115.             {
  116.                 squareMatrix[currentRowBomb, currentColBomb - 1] -= bombDamage;
  117.             }
  118.             if (IsValid(currentRowBomb, currentColBomb + 1, squareMatrix) && squareMatrix[currentRowBomb, currentColBomb + 1] > 0)
  119.             {
  120.                 squareMatrix[currentRowBomb, currentColBomb + 1] -= bombDamage;
  121.             }
  122.  
  123.             squareMatrix[currentRowBomb, currentColBomb] -= bombDamage;
  124.  
  125.             return squareMatrix;
  126.         }
  127.  
  128.         public static bool IsValid(int row, int col, int[,] squareMatrix)
  129.         {
  130.             return (row >= 0 && row < squareMatrix.GetLength(0) && col >= 0 && col < squareMatrix.GetLength(1));
  131.         }
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement