Advertisement
alexbancheva

Bombs_RetakeExam_17Dec2018

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