Advertisement
WindFell

Bombs

Jan 3rd, 2019
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Bombs
  5. {
  6.     static int[][] field;
  7.  
  8.     static void Main(string[] args)
  9.     {
  10.         int size = int.Parse(Console.ReadLine());
  11.         field = new int[size][];
  12.         ReadField();
  13.         string[] coords = Console.ReadLine()
  14.             .Split();
  15.  
  16.         foreach (var coord in coords)
  17.         {
  18.             int[] coordArgs = coord
  19.                 .Split(',')
  20.                 .Select(int.Parse)
  21.                 .ToArray();
  22.             int row = coordArgs[0];
  23.             int col = coordArgs[1];
  24.             BombCells(row, col);
  25.         }
  26.  
  27.         PrintCellInfo();
  28.         PrintField();
  29.     }
  30.  
  31.     private static void BombCells(int row, int col)
  32.     {
  33.         int damage = field[row][col];
  34.  
  35.         if (damage > 0)
  36.         {
  37.             BombCell(row - 1, col - 1, damage);
  38.             BombCell(row - 1, col, damage);
  39.             BombCell(row - 1, col + 1, damage);
  40.             BombCell(row, col - 1, damage);
  41.             BombCell(row, col + 1, damage);
  42.             BombCell(row + 1, col - 1, damage);
  43.             BombCell(row + 1, col, damage);
  44.             BombCell(row + 1, col + 1, damage);
  45.             field[row][col] = 0;
  46.         }
  47.     }
  48.  
  49.     private static void BombCell(int row, int col, int damage)
  50.     {
  51.         if (row >= 0 && row < field.Length &&
  52.             col >= 0 && col < field.Length &&
  53.             field[row][col] > 0)
  54.         {
  55.             field[row][col] -= damage;
  56.         }
  57.     }
  58.  
  59.     static void ReadField()
  60.     {
  61.         for (int i = 0; i < field.Length; i++)
  62.         {
  63.             int[] row = Console.ReadLine()
  64.                 .Split()
  65.                 .Select(int.Parse)
  66.                 .ToArray();
  67.             field[i] = row;
  68.         }
  69.     }
  70.  
  71.     static void PrintField()
  72.     {
  73.         for (int row = 0; row < field.Length; row++)
  74.         {
  75.             Console.WriteLine(string.Join(" ", field[row]));
  76.         }
  77.     }
  78.  
  79.     static void PrintCellInfo()
  80.     {
  81.         int aliveCellsCount = 0;
  82.         int aliveCellsSum = 0;
  83.  
  84.         for (int row = 0; row < field.Length; row++)
  85.         {
  86.             int[] aliveCells = field[row]
  87.                 .Where(x => x > 0)
  88.                 .ToArray();
  89.             aliveCellsCount += aliveCells.Length;
  90.             aliveCellsSum += aliveCells.Sum();
  91.         }
  92.  
  93.         Console.WriteLine($"Alive cells: {aliveCellsCount}");
  94.         Console.WriteLine($"Sum: {aliveCellsSum}");
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement