Advertisement
WindFell

Bombs

Jan 3rd, 2019
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.96 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Bombs
  5. {
  6.     static int[,] matrix;
  7.  
  8.     static void Main()
  9.     {
  10.         int size = int.Parse(Console.ReadLine());
  11.  
  12.         matrix = new int[size, size];
  13.  
  14.         for (int i = 0; i < size; i++)
  15.         {
  16.             int[] row = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  17.  
  18.             for (int j = 0; j < row.Length; j++)
  19.             {
  20.                 matrix[i, j] = row[j];
  21.             }
  22.         }
  23.  
  24.         string[] coordinates = Console.ReadLine().Split(' ');
  25.  
  26.         if (size > 1)
  27.         {
  28.             for (int i = 0; i < coordinates.Length; i++)
  29.             {
  30.                 string[] coordinate = coordinates[i].Split(',');
  31.                 int row = int.Parse(coordinate[0]);
  32.                 int column = int.Parse(coordinate[1]);
  33.  
  34.                 if (row == 0)
  35.                 {
  36.                     if (column == 0)
  37.                     {
  38.                         if (matrix[row, column] > 0)
  39.                         {
  40.                             ChangeCell(row, column, row, column + 1);
  41.                             ChangeCell(row, column, row + 1, column + 1);
  42.                             ChangeCell(row, column, row + 1, column);
  43.                             matrix[row, column] = 0;
  44.                         }
  45.                     }
  46.                     else if (column == size - 1)
  47.                     {
  48.                         if (matrix[row, column] > 0)
  49.                         {
  50.                             ChangeCell(row, column, row + 1, column);
  51.                             ChangeCell(row, column, row + 1, column - 1);
  52.                             ChangeCell(row, column, row, column - 1);
  53.                             matrix[row, column] = 0;
  54.                         }
  55.                     }
  56.                     else
  57.                     {
  58.                         if (matrix[row, column] > 0)
  59.                         {
  60.                             ChangeCell(row, column, row, column + 1);
  61.                             ChangeCell(row, column, row + 1, column + 1);
  62.                             ChangeCell(row, column, row + 1, column);
  63.                             ChangeCell(row, column, row + 1, column - 1);
  64.                             ChangeCell(row, column, row, column - 1);
  65.                             matrix[row, column] = 0;
  66.                         }
  67.                     }
  68.                 }
  69.                 else if (row == size - 1)
  70.                 {
  71.                     if (column == 0)
  72.                     {
  73.                         if (matrix[row, column] > 0)
  74.                         {
  75.                             ChangeCell(row, column, row - 1, column);
  76.                             ChangeCell(row, column, row - 1, column + 1);
  77.                             ChangeCell(row, column, row, column + 1);
  78.                             matrix[row, column] = 0;
  79.                         }
  80.                     }
  81.                     else if (column == size - 1)
  82.                     {
  83.                         if (matrix[row, column] > 0)
  84.                         {
  85.                             ChangeCell(row, column, row - 1, column);
  86.                             ChangeCell(row, column, row - 1, column - 1);
  87.                             ChangeCell(row, column, row, column - 1);
  88.                             matrix[row, column] = 0;
  89.                         }
  90.                     }
  91.                     else
  92.                     {
  93.                         if (matrix[row, column] > 0)
  94.                         {
  95.                             ChangeCell(row, column, row - 1, column);
  96.                             ChangeCell(row, column, row - 1, column + 1);
  97.                             ChangeCell(row, column, row, column + 1);
  98.                             ChangeCell(row, column, row, column - 1);
  99.                             ChangeCell(row, column, row - 1, column - 1);
  100.                             matrix[row, column] = 0;
  101.                         }
  102.                     }
  103.                 }
  104.                 else
  105.                 {
  106.                     if (column == 0)
  107.                     {
  108.                         if (matrix[row, column] > 0)
  109.                         {
  110.                             ChangeCell(row, column, row - 1, column);
  111.                             ChangeCell(row, column, row - 1, column + 1);
  112.                             ChangeCell(row, column, row, column + 1);
  113.                             ChangeCell(row, column, row + 1, column + 1);
  114.                             ChangeCell(row, column, row + 1, column);
  115.                             matrix[row, column] = 0;
  116.                         }
  117.                     }
  118.                     else if (column == size - 1)
  119.                     {
  120.                         if (matrix[row, column] > 0)
  121.                         {
  122.                             ChangeCell(row, column, row - 1, column);
  123.                             ChangeCell(row, column, row - 1, column - 1);
  124.                             ChangeCell(row, column, row, column - 1);
  125.                             ChangeCell(row, column, row + 1, column - 1);
  126.                             ChangeCell(row, column, row + 1, column);
  127.                             matrix[row, column] = 0;
  128.                         }
  129.                     }
  130.                     else
  131.                     {
  132.                         if (matrix[row, column] > 0)
  133.                         {
  134.                             ChangeCell(row, column, row - 1, column);
  135.                             ChangeCell(row, column, row - 1, column + 1);
  136.                             ChangeCell(row, column, row, column + 1);
  137.                             ChangeCell(row, column, row + 1, column + 1);
  138.                             ChangeCell(row, column, row + 1, column);
  139.                             ChangeCell(row, column, row + 1, column - 1);
  140.                             ChangeCell(row, column, row, column - 1);
  141.                             ChangeCell(row, column, row - 1, column - 1);
  142.                             matrix[row, column] = 0;
  143.                         }
  144.                     }
  145.                 }
  146.             }
  147.         }
  148.  
  149.         int cellsAliveCount = 0;
  150.         int cellsAliveSum = 0;
  151.  
  152.         for (int i = 0; i < size; i++)
  153.         {
  154.             for (int j = 0; j < size; j++)
  155.             {
  156.                 if (matrix[i, j] > 0)
  157.                 {
  158.                     cellsAliveCount++;
  159.                     cellsAliveSum += matrix[i, j];
  160.                 }
  161.             }
  162.         }
  163.  
  164.         Console.WriteLine($"Alive cells: {cellsAliveCount}");
  165.         Console.WriteLine($"Sum: {cellsAliveSum}");
  166.  
  167.         for (int i = 0; i < size; i++)
  168.         {
  169.             for (int j = 0; j < size; j++)
  170.             {
  171.                 Console.Write($"{matrix[i, j]} ");
  172.             }
  173.  
  174.             Console.WriteLine();
  175.         }
  176.     }
  177.  
  178.     static void ChangeCell(int bombRow, int bombColumn, int row, int column)
  179.     {
  180.         if (matrix[row, column] > 0)
  181.         {
  182.             matrix[row, column] -= matrix[bombRow, bombColumn];
  183.         }
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement