Advertisement
Daniel_007

08. Bombs

May 30th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace ConsoleApp5
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int inputRowsColums = int.Parse(Console.ReadLine());
  11.             int rows = inputRowsColums;
  12.             int colums = rows;
  13.             int[,] matrix = new int[rows, colums];
  14.             //inputMatrix
  15.             for (int row = 0; row < rows; row++)
  16.             {
  17.                 int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
  18.                 for (int col = 0; col < colums; col++)
  19.                 {
  20.                     matrix[row, col] = input[col];
  21.                 }
  22.             }
  23.  
  24.             string[] bombs = Console.ReadLine().Split().ToArray();
  25.             for (int i = 0; i < bombs.Length; i++)
  26.             {
  27.                 int[] rowColBomb = bombs[i].Split(",").Select(int.Parse).ToArray();
  28.                 int rowBomb = rowColBomb[0];
  29.                 int colBomb = rowColBomb[1];
  30.                 int bombPower = matrix[rowBomb, colBomb];
  31.                 if (matrix[rowBomb, colBomb] <= 0)
  32.                 {
  33.                     continue;
  34.                 }
  35.                 for (int row = Math.Max(rowBomb - 1, 0); row <= Math.Min(rowBomb + 1, rows-1); row++)
  36.                 {
  37.                     for (int col = Math.Max(colBomb - 1, 0); col <= Math.Min(colBomb + 1, colums-1); col++)
  38.                     {
  39.                         if (matrix[row, col] <= 0)
  40.                         {
  41.                             continue;
  42.                         }
  43.                         matrix[row, col] -= bombPower;
  44.                     }
  45.                 }
  46.  
  47.                 matrix[rowBomb, colBomb] = 0;
  48.             }
  49.             //checkActiveCells and calculate sum
  50.             int active = 0;
  51.             int sum = 0;
  52.             for (int row = 0; row < rows; row++)
  53.             {
  54.                 for (int col = 0; col < colums; col++)
  55.                 {
  56.                     if (matrix[row, col] > 0)
  57.                     {
  58.                         active++;
  59.                         sum += matrix[row, col];
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             Console.WriteLine($"Alive cells: {active}");
  65.             Console.WriteLine($"Sum: {sum}");
  66.             //printMatrix
  67.             for (int row = 0; row < rows; row++)
  68.             {
  69.                 for (int col = 0; col < colums-1; col++)
  70.                 {
  71.  
  72.                     Console.Write(matrix[row, col] + " ");
  73.                 }
  74.  
  75.                 Console.Write(matrix[row, colums-1]);
  76.  
  77.                Console.WriteLine();
  78.             }
  79.  
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement