Advertisement
Guest User

Untitled

a guest
May 5th, 2019
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace _8._Bombs
  7. {
  8.     class Program
  9.     {
  10.         static int[][] matrix;
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             matrix = new int[n][];
  15.  
  16.             FillMatrix();
  17.  
  18.             string[] bombs = Console.ReadLine().Split();
  19.             Queue<string> coordinate = new Queue<string>(bombs);
  20.  
  21.             Explosion(coordinate);
  22.  
  23.             int aliveCount = 0;
  24.             long sum = 0;
  25.             for (int row = 0; row < matrix.Length; row++)
  26.             {
  27.                 for (int col = 0; col < matrix[row].Length; col++)
  28.                 {
  29.                     if (matrix[row][col] > 0)
  30.                     {
  31.                         aliveCount++;
  32.                         sum += matrix[row][col];
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             Console.WriteLine($"Alive cells: {aliveCount}");
  38.             Console.WriteLine($"Sum: {sum}");
  39.  
  40.             PrintMatrix();
  41.         }
  42.         private static void Explosion(Queue<string> coordinate)
  43.         {
  44.             while (coordinate.Count > 0)
  45.             {
  46.                 int[] input = coordinate.Dequeue().Split(',').Select(int.Parse).ToArray();
  47.                 int r = input[0];
  48.                 int c = input[1];
  49.                 if (matrix[r][c] > 0)
  50.                 {
  51.                     int bombPower = matrix[r][c];
  52.  
  53.                     if (IsToExplode(r - 1, c - 1)) matrix[r - 1][c - 1] -= bombPower;
  54.                     if (IsToExplode(r - 1, c + 1)) matrix[r - 1][c + 1] -= bombPower;
  55.                     if (IsToExplode(r, c - 1)) matrix[r][c - 1] -= bombPower;
  56.                     if (IsToExplode(r, c + 1)) matrix[r][c + 1] -= bombPower;
  57.                     if (IsToExplode(r + 1, c - 1)) matrix[r + 1][c - 1] -= bombPower;
  58.                     if (IsToExplode(r + 1, c + 1)) matrix[r + 1][c + 1] -= bombPower;
  59.                     if (IsToExplode(r - 1, c)) matrix[r - 1][c] -= bombPower;
  60.                     if (IsToExplode(r + 1, c)) matrix[r + 1][c] -= bombPower;
  61.  
  62.                     matrix[r][c] = 0;
  63.                 }              
  64.             }
  65.         }
  66.         private static bool IsToExplode(int r, int c)
  67.         {
  68.             return r >= 0 && r < matrix.Length && c >= 0 && c < matrix[r].Length && matrix[r][c] > 0;
  69.         }
  70.         private static void PrintMatrix()
  71.         {
  72.             for (int row = 0; row < matrix.Length; row++)
  73.             {
  74.                 Console.WriteLine(string.Join(" ",matrix[row]));
  75.             }
  76.         }
  77.         private static void FillMatrix()
  78.         {
  79.             for (int row = 0; row < matrix.Length; row++)
  80.             {
  81.                 matrix[row] = new int[matrix.Length];
  82.                 matrix[row] = Console.ReadLine().Split().Select(int.Parse).ToArray();
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement