Advertisement
Guest User

Untitled

a guest
May 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Bomb
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int size = int.Parse(Console.ReadLine());
  14.  
  15.             int[,] array = new int[size, size];
  16.  
  17.             for (int i = 0; i < size; i++)
  18.             {
  19.                 int[] arrayData = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  20.  
  21.                 for (int j = 0; j < size; j++)
  22.                 {
  23.                     array[i, j] = arrayData[j];
  24.                 }
  25.             }
  26.  
  27.             string[] inputCoords = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  28.  
  29.             for (int i = 0; i < inputCoords.Length; i++)
  30.             {
  31.                 string[] coords = inputCoords[i].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  32.                 int rowCord = int.Parse(coords[0]);
  33.                 int colCord = int.Parse(coords[1]);
  34.  
  35.                 int bombPower = array[rowCord, colCord];
  36.  
  37.  
  38.                 if (array[rowCord, colCord] > 0)
  39.                 {
  40.                     // up
  41.                     if (IsInside(array, rowCord - 1, colCord) && array[rowCord - 1, colCord] > 0)
  42.                     {
  43.                         array[rowCord - 1, colCord] -= bombPower;
  44.                     }
  45.  
  46.                     //down
  47.                     if (IsInside(array, rowCord + 1, colCord) && array[rowCord + 1, colCord] > 0)
  48.                     {
  49.                         array[rowCord + 1, colCord] -= bombPower;
  50.                     }
  51.  
  52.                     //left
  53.                     if (IsInside(array, rowCord, colCord + 1) && array[rowCord, colCord + 1] > 0)
  54.                     {
  55.                         array[rowCord, colCord + 1] -= bombPower;
  56.                     }
  57.  
  58.                     //right
  59.                     if (IsInside(array, rowCord, colCord - 1) && array[rowCord, colCord - 1] > 0)
  60.                     {
  61.                         array[rowCord, colCord - 1] -= bombPower;
  62.                     }
  63.  
  64.                     // up right diagonal
  65.                     if (IsInside(array, rowCord - 1, colCord + 1) && array[rowCord - 1, colCord + 1] > 0)
  66.                     {
  67.                         array[rowCord - 1, colCord + 1] -= bombPower;
  68.                     }
  69.  
  70.                     // up left diagonal
  71.  
  72.                     if (IsInside(array, rowCord - 1, colCord - 1) && array[rowCord - 1, colCord - 1] > 0)
  73.                     {
  74.                         array[rowCord - 1, colCord - 1] -= bombPower;
  75.  
  76.                     }
  77.  
  78.                     // down right diagonal
  79.  
  80.                     if (IsInside(array, rowCord + 1, colCord + 1) && array[rowCord + 1, colCord + 1] > 0)
  81.                     {
  82.                         array[rowCord + 1, colCord + 1] -= bombPower;
  83.  
  84.                     }
  85.  
  86.                     // down left diagonal
  87.                     if (IsInside(array, rowCord + 1, colCord - 1) && array[rowCord + 1, colCord - 1] > 0)
  88.                     {
  89.                         array[rowCord + 1, colCord - 1] -= bombPower;
  90.  
  91.                     }
  92.                     array[rowCord, colCord] = 0;
  93.                 }
  94.  
  95.             }
  96.  
  97.             int aliveCells = 0;
  98.             int sum = 0;
  99.  
  100.             for (int i = 0; i < array.GetLength(0); i++)
  101.             {
  102.                 for (int j = 0; j < array.GetLength(1); j++)
  103.                 {
  104.  
  105.                     if (array[i, j] > 0)
  106.                     {
  107.                         sum += array[i, j];
  108.                         aliveCells++;
  109.                     }
  110.                 }
  111.             }
  112.  
  113.             Console.WriteLine($"Alive cells: {aliveCells}");
  114.             Console.WriteLine($"Sum: {sum}");
  115.  
  116.             for (int i = 0; i < array.GetLength(0); i++)
  117.             {
  118.                 for (int j = 0; j < array.GetLength(1); j++)
  119.                 {
  120.                     Console.Write(array[i, j] + " ");
  121.  
  122.                 }
  123.                 Console.WriteLine();
  124.             }
  125.         }
  126.  
  127.         private static bool IsInside(int[,] array, int row, int col)
  128.         {
  129.             return row >= 0 && row < array.GetLength(0) && col >= 0 && col < array.GetLength(1);
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement