Advertisement
DyNaMiXx7

Bombs

Jun 14th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _08Bombs
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int matrixSize = int.Parse(Console.ReadLine());
  11.             int[,] matrix = new int[matrixSize, matrixSize];
  12.  
  13.             // Fill the matrix
  14.             for (int row = 0; row < matrixSize; row++)
  15.             {
  16.                 int[] values = Console.ReadLine()
  17.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  18.                     .Select(int.Parse)
  19.                     .ToArray();
  20.  
  21.                 for (int col = 0; col < matrixSize; col++)
  22.                 {
  23.                     matrix[row, col] = values[col];
  24.                 }
  25.             }
  26.  
  27.             string[] bombCoordinates = Console.ReadLine()
  28.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  29.  
  30.             for (int i = 0; i < bombCoordinates.Length; i++)
  31.             {
  32.                 int[] bomb = bombCoordinates[i]
  33.                     .Split(",")
  34.                     .Select(int.Parse)
  35.                     .ToArray();
  36.  
  37.                 int bombRow = bomb[0];
  38.                 int bombCol = bomb[1];
  39.  
  40.                 if (IsInside(matrix, bombRow, bombCol))
  41.                 {
  42.                     if (IsInside(matrix, bombRow - 1, bombCol - 1) && matrix[bombRow - 1, bombCol - 1] > 0)
  43.                     {
  44.                         matrix[bombRow - 1, bombCol - 1] -= matrix[bombRow, bombCol];
  45.                     }
  46.  
  47.                     if (IsInside(matrix, bombRow - 1, bombCol) && matrix[bombRow - 1, bombCol] > 0)
  48.                     {
  49.                         matrix[bombRow - 1, bombCol] -= matrix[bombRow, bombCol];
  50.                     }
  51.  
  52.                     if (IsInside(matrix, bombRow - 1, bombCol + 1) && matrix[bombRow - 1, bombCol + 1] > 0)
  53.                     {
  54.                         matrix[bombRow - 1, bombCol + 1] -= matrix[bombRow, bombCol];
  55.                     }
  56.  
  57.                     if (IsInside(matrix, bombRow, bombCol - 1) && matrix[bombRow, bombCol - 1] > 0)
  58.                     {
  59.                         matrix[bombRow, bombCol - 1] -= matrix[bombRow, bombCol];
  60.                     }
  61.  
  62.                     if (IsInside(matrix, bombRow, bombCol + 1) && matrix[bombRow, bombCol + 1] > 0)
  63.                     {
  64.                         matrix[bombRow, bombCol + 1] -= matrix[bombRow, bombCol];
  65.                     }
  66.  
  67.                     if (IsInside(matrix, bombRow + 1, bombCol - 1) && matrix[bombRow + 1, bombCol - 1] > 0)
  68.                     {
  69.                         matrix[bombRow + 1, bombCol - 1] -= matrix[bombRow, bombCol];
  70.                     }
  71.  
  72.                     if (IsInside(matrix, bombRow + 1, bombCol) && matrix[bombRow + 1, bombCol] > 0)
  73.                     {
  74.                         matrix[bombRow + 1, bombCol] -= matrix[bombRow, bombCol];
  75.                     }
  76.  
  77.                     if (IsInside(matrix, bombRow + 1, bombCol + 1) && matrix[bombRow + 1, bombCol + 1] > 0)
  78.                     {
  79.                         matrix[bombRow + 1, bombCol + 1] -= matrix[bombRow, bombCol];
  80.                     }
  81.  
  82.                     matrix[bombRow, bombCol] = 0;
  83.                 }
  84.             }
  85.  
  86.             int aliveCells = 0;
  87.             int sum = 0;
  88.  
  89.             for (int row = 0; row < matrixSize; row++)
  90.             {
  91.                 for (int col = 0; col < matrixSize; col++)
  92.                 {
  93.                     if (matrix[row, col] > 0)
  94.                     {
  95.                         aliveCells++;
  96.                         sum += matrix[row, col];
  97.                     }
  98.                 }
  99.             }
  100.  
  101.             Console.WriteLine($"Alive cells: {aliveCells}");
  102.             Console.WriteLine($"Sum: {sum}");
  103.  
  104.             // Print the new matrix
  105.             for (int row = 0; row < matrixSize; row++)
  106.             {
  107.                 for (int col = 0; col < matrixSize; col++)
  108.                 {
  109.                     Console.Write(matrix[row, col] + " ");
  110.                 }
  111.  
  112.                 Console.WriteLine();
  113.             }
  114.         }
  115.  
  116.         // Method, which validates coordinates
  117.         private static bool IsInside(int[,] matrix, int desiredRow, int desiredCol)
  118.         {
  119.             return desiredRow < matrix.GetLength(0) && desiredCol < matrix.GetLength(1) &&
  120.                 desiredRow >= 0 && desiredCol >= 0;
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement