Advertisement
yanass

Bombs - Multidimensional Arrays

Oct 5th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Bombs
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             int size = int.Parse(Console.ReadLine());
  11.  
  12.             int[,] matrix = new int[size, size];
  13.  
  14.             CreateMatrix(matrix);
  15.  
  16.             string[] bombCoordinates = Console.ReadLine().Split();
  17.  
  18.             for (int i = 0; i < bombCoordinates.Length; i++)
  19.             {
  20.                 int[] coordinates = bombCoordinates[i].Split(',').Select(int.Parse).ToArray();
  21.  
  22.                 Explode(coordinates[0], coordinates[1], matrix);
  23.             }
  24.  
  25.             int countAlive = matrix.Cast<int>().Where(x => x > 0).Count();
  26.             int sum = matrix.Cast<int>().Where(x => x > 0).Sum();
  27.  
  28.             Console.WriteLine($"Alive cells: {countAlive}");
  29.             Console.WriteLine($"Sum: {sum}");
  30.  
  31.             for (int r = 0; r < matrix.GetLength(0); r++)
  32.             {
  33.                 for (int c = 0; c < matrix.GetLength(1); c++)
  34.                 {
  35.                     Console.Write($"{matrix[r, c]} ");
  36.                 }
  37.                 Console.WriteLine();
  38.             }
  39.  
  40.         }
  41.         static void CreateMatrix(int[,] matrix)
  42.         {
  43.             for (int row = 0; row < matrix.GetLength(0); row++)
  44.             {
  45.                 int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
  46.  
  47.                 for (int col = 0; col < matrix.GetLength(1); col++)
  48.                 {
  49.                     matrix[row, col] = input[col];
  50.                 }
  51.             }
  52.         }
  53.         static bool IsValid(int[,] matrix, int row, int col)
  54.         {
  55.             return row >= 0
  56.                 && row < matrix.GetLength(0)
  57.                 && col >= 0
  58.                 && col < matrix.GetLength(1);
  59.         }
  60.         static void Explode(int x, int y, int[,] matrix)
  61.         {
  62.             if (matrix[x, y] > 0)
  63.             {
  64.                 int bombPower = matrix[x, y];
  65.  
  66.                 PositionsBoom(x - 1, y - 1, bombPower, matrix);
  67.                 PositionsBoom(x - 1, y, bombPower, matrix);
  68.                 PositionsBoom(x - 1, y + 1, bombPower, matrix);
  69.                 PositionsBoom(x, y - 1, bombPower, matrix);
  70.                 PositionsBoom(x, y + 1, bombPower, matrix);
  71.                 PositionsBoom(x + 1, y - 1, bombPower, matrix);
  72.                 PositionsBoom(x + 1, y, bombPower, matrix);
  73.                 PositionsBoom(x + 1, y + 1, bombPower, matrix);
  74.  
  75.                 matrix[x, y] = 0;
  76.             }
  77.         }
  78.  
  79.         static void PositionsBoom(int posRow, int posCol, int bombPower, int[,] matrix)
  80.         {
  81.             if (IsValid(matrix, posRow, posCol) && matrix[posRow, posCol] > 0)
  82.             {
  83.                 matrix[posRow, posCol] -= bombPower;
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement