Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. namespace _8._Bombs
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     class StartUp
  7.     {
  8.         public static void Main()
  9.         {
  10.             int sizeMatrix = int.Parse(Console.ReadLine());
  11.             int[,] matrix = new int[sizeMatrix, sizeMatrix];
  12.  
  13.             for (int i = 0; i < matrix.GetLength(0); i++)
  14.             {
  15.                 int[] rowMatrix = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  16.                 for (int j = 0; j < matrix.GetLength(1); j++)
  17.                 {
  18.                     matrix[i, j] = rowMatrix[j];
  19.                 }
  20.             }
  21.  
  22.             int[] bombs = Console.ReadLine().Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  23.  
  24.             for (int i = 0; i < bombs.Length; i += 2)
  25.             {
  26.                 int rowBomb = bombs[i];
  27.                 int colBomb = bombs[i + 1];
  28.                 int power;
  29.  
  30.                 bool isValid = GetIsValid(matrix, rowBomb, colBomb);
  31.                 if (isValid)
  32.                 {
  33.                     power = matrix[rowBomb, colBomb];
  34.                     if (matrix[rowBomb, colBomb] > 0)
  35.                     {
  36.                         matrix[rowBomb, colBomb] = 0;
  37.                     }
  38.  
  39.                     isValid = GetIsValid(matrix, rowBomb, 0, colBomb, -1);
  40.                     if (isValid)
  41.                     {
  42.                         if (matrix[rowBomb, colBomb - 1] > 0)
  43.                         {
  44.                             matrix[rowBomb, colBomb - 1] -= power;
  45.                         }
  46.                     }
  47.  
  48.                     isValid = GetIsValid(matrix, rowBomb, -1, colBomb, -1);
  49.                     if (isValid)
  50.                     {
  51.                         if (matrix[rowBomb - 1, colBomb - 1] > 0)
  52.                         {
  53.                             matrix[rowBomb - 1, colBomb - 1] -= power;
  54.                         }
  55.                     }
  56.  
  57.                     isValid = GetIsValid(matrix, rowBomb, -1, colBomb, 0);
  58.                     if (isValid)
  59.                     {
  60.                         if (matrix[rowBomb - 1, colBomb] > 0)
  61.                         {
  62.                             matrix[rowBomb - 1, colBomb] -= power;
  63.                         }
  64.                     }
  65.  
  66.                     isValid = GetIsValid(matrix, rowBomb, -1, colBomb, +1);
  67.                     if (isValid)
  68.                     {
  69.                         if (matrix[rowBomb - 1, colBomb + 1] > 0)
  70.                         {
  71.                             matrix[rowBomb - 1, colBomb + 1] -= power;
  72.                         }
  73.                     }
  74.  
  75.                     isValid = GetIsValid(matrix, rowBomb, 0, colBomb, +1);
  76.                     if (isValid)
  77.                     {
  78.                         if (matrix[rowBomb, colBomb + 1] > 0)
  79.                         {
  80.                             matrix[rowBomb, colBomb + 1] -= power;
  81.                         }
  82.                     }
  83.  
  84.                     isValid = GetIsValid(matrix, rowBomb, +1, colBomb, +1);
  85.                     if (isValid)
  86.                     {
  87.                         if (matrix[rowBomb + 1, colBomb + 1] > 0)
  88.                         {
  89.                             matrix[rowBomb + 1, colBomb + 1] -= power;
  90.                         }
  91.                     }
  92.  
  93.                     isValid = GetIsValid(matrix, rowBomb, +1, colBomb, 0);
  94.                     if (isValid)
  95.                     {
  96.                         if (matrix[rowBomb + 1, colBomb] > 0)
  97.                         {
  98.                             matrix[rowBomb + 1, colBomb] -= power;
  99.                         }
  100.                     }
  101.  
  102.                     isValid = GetIsValid(matrix, rowBomb, +1, colBomb, -1);
  103.                     if (isValid)
  104.                     {
  105.                         if (matrix[rowBomb + 1, colBomb - 1] > 0)
  106.                         {
  107.                             matrix[rowBomb + 1, colBomb - 1] -= power;
  108.                         }
  109.                     }
  110.                 }
  111.             }
  112.  
  113.             int count = 0;
  114.             int sum = 0;
  115.  
  116.             for (int i = 0; i < matrix.GetLength(0); i++)
  117.             {
  118.                 for (int j = 0; j < matrix.GetLength(1); j++)
  119.                 {
  120.                     if (matrix[i, j] > 0)
  121.                     {
  122.                         count++;
  123.                         sum += matrix[i, j];
  124.                     }
  125.                 }
  126.             }
  127.  
  128.             Console.WriteLine($"Alive cells: {count}");
  129.             Console.WriteLine($"Sum: {sum}");
  130.             for (int i = 0; i < matrix.GetLength(0); i++)
  131.             {
  132.                 for (int j = 0; j < matrix.GetLength(1); j++)
  133.                 {
  134.                     Console.Write(matrix[i, j] + " ");
  135.                 }
  136.  
  137.                 Console.WriteLine();
  138.             }
  139.         }
  140.  
  141.         private static bool GetIsValid(int[,] matrix, int rowBomb, int v1, int colBomb, int v2)
  142.         {
  143.             if (rowBomb + v1 >= 0 && rowBomb + v1 < matrix.GetLength(0) && colBomb + v2 >= 0 && colBomb + v2 < matrix.GetLength(1))
  144.             {
  145.                 return true;
  146.             }
  147.             else
  148.             {
  149.                 return false;
  150.             }
  151.         }
  152.  
  153.         private static bool GetIsValid(int[,] matrix, int rowBomb, int colBomb)
  154.         {
  155.             if (rowBomb >= 0 && rowBomb < matrix.GetLength(0) && colBomb >= 0 && colBomb < matrix.GetLength(1))
  156.             {
  157.                 return true;
  158.             }
  159.             else
  160.             {
  161.                 return false;
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement