Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.33 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Problem_8._Bombs
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int dimensions = int.Parse(Console.ReadLine());
  12.  
  13.             int[][] matrix = new int[dimensions][];
  14.  
  15.             for (int i = 0; i < matrix.Length; i++)
  16.             {
  17.                 matrix[i] = Console.ReadLine().Split().Select(int.Parse).ToArray();
  18.             }
  19.  
  20.             string[] bombsCoors = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  21.  
  22.             for (int i = 0; i < bombsCoors.Length; i++)
  23.             {
  24.                 int[] indexes = bombsCoors[i].Split(',').Select(int.Parse).ToArray();
  25.  
  26.                 int bombRow = indexes[0];
  27.                 int bombCol = indexes[1];
  28.  
  29.                 int nextBombRow = -1;
  30.                 int nextBombCol = -1;
  31.  
  32.                 if (i != bombsCoors.Length - 1)
  33.                 {
  34.                     int[] nextIndexes = bombsCoors[i + 1].Split(',').Select(int.Parse).ToArray();
  35.                     nextBombRow = nextIndexes[0];
  36.                     nextBombCol = nextIndexes[1];
  37.                 }
  38.  
  39.                 if (IsInside(matrix, bombRow, bombCol - 1) && !IsBomb(matrix, bombRow, bombCol - 1, nextBombRow, nextBombCol))
  40.                 {
  41.                     if (IsPositive(matrix, bombRow, bombCol - 1))
  42.                     {
  43.                         matrix[bombRow][bombCol - 1] -= matrix[bombRow][bombCol];
  44.                     }
  45.                 }
  46.  
  47.                 if (IsInside(matrix, bombRow - 1, bombCol - 1) && !IsBomb(matrix, bombRow - 1, bombCol - 1, nextBombRow, nextBombCol))
  48.                 {
  49.                     if (IsPositive(matrix, bombRow - 1, bombCol - 1))
  50.                     {
  51.                         matrix[bombRow - 1][bombCol - 1] -= matrix[bombRow][bombCol];
  52.                     }
  53.                 }
  54.  
  55.                 if (IsInside(matrix, bombRow - 1, bombCol) && !IsBomb(matrix, bombRow - 1, bombCol, nextBombRow, nextBombCol))
  56.                 {
  57.                     if (IsPositive(matrix, bombRow - 1, bombCol))
  58.                     {
  59.                         matrix[bombRow - 1][bombCol] -= matrix[bombRow][bombCol];
  60.                     }
  61.                 }
  62.  
  63.                 if (IsInside(matrix, bombRow - 1, bombCol + 1) && !IsBomb(matrix, bombRow - 1, bombCol + 1, nextBombRow, nextBombCol))
  64.                 {
  65.                     if (IsPositive(matrix, bombRow - 1, bombCol + 1))
  66.                     {
  67.                         matrix[bombRow - 1][bombCol + 1] -= matrix[bombRow][bombCol];
  68.                     }
  69.                 }
  70.  
  71.                 if (IsInside(matrix, bombRow, bombCol + 1) && !IsBomb(matrix, bombRow, bombCol + 1, nextBombRow, nextBombCol))
  72.                 {
  73.                     if (IsPositive(matrix, bombRow, bombCol + 1))
  74.                     {
  75.                         matrix[bombRow][bombCol + 1] -= matrix[bombRow][bombCol];
  76.                     }
  77.                 }
  78.  
  79.                 if (IsInside(matrix, bombRow + 1, bombCol + 1) && !IsBomb(matrix, bombRow + 1, bombCol + 1, nextBombRow, nextBombCol))
  80.                 {
  81.                     if (IsPositive(matrix, bombRow + 1, bombCol + 1))
  82.                     {
  83.                         matrix[bombRow + 1][bombCol + 1] -= matrix[bombRow][bombCol];
  84.                     }
  85.                 }
  86.  
  87.                 if (IsInside(matrix, bombRow + 1, bombCol) && !IsBomb(matrix, bombRow + 1, bombCol, nextBombRow, nextBombCol))
  88.                 {
  89.                     if (IsPositive(matrix, bombRow + 1, bombCol))
  90.                     {
  91.                         matrix[bombRow + 1][bombCol] -= matrix[bombRow][bombCol];
  92.                     }
  93.                 }
  94.  
  95.                 if (IsInside(matrix, bombRow + 1, bombCol - 1) && !IsBomb(matrix, bombRow + 1, bombCol - 1, nextBombRow, nextBombCol))
  96.                 {
  97.                     if (IsPositive(matrix, bombRow + 1, bombCol - 1))
  98.                     {
  99.                         matrix[bombRow + 1][bombCol - 1] -= matrix[bombRow][bombCol];
  100.                     }
  101.                 }
  102.  
  103.                 matrix[bombRow][bombCol] = 0;
  104.             }
  105.  
  106.             int alive = 0;
  107.             int sum = 0;
  108.  
  109.             for (int i = 0; i < matrix.Length; i++)
  110.             {
  111.                 for (int j = 0; j < matrix[i].Length; j++)
  112.                 {
  113.                     if (matrix[i][j] > 0)
  114.                     {
  115.                         alive++;
  116.                         sum += matrix[i][j];
  117.                     }
  118.                 }
  119.             }
  120.  
  121.             Console.WriteLine($"Alive cells: {alive}");
  122.             Console.WriteLine($"Sum: {sum}");
  123.  
  124.             foreach (var item in matrix)
  125.             {
  126.                 Console.WriteLine(string.Join(" ", item));
  127.             }
  128.         }
  129.  
  130.         private static bool IsPositive(int[][] matrix, int bombRow, int bombCol)
  131.         {
  132.             return matrix[bombRow][bombCol] > 0;
  133.         }
  134.  
  135.         private static bool IsBomb(int[][] matrix, int bombRow, int bombCol, int nextBombRow, int nextBombCol)
  136.         {
  137.             return bombRow == nextBombRow && bombCol == nextBombCol;
  138.         }
  139.  
  140.         private static bool IsInside(int[][] matrix, int row, int col)
  141.         {
  142.             return row >= 0 && row < matrix.Length && col >= 0 && col < matrix[row].Length;
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement