Advertisement
Guest User

Untitled

a guest
Nov 24th, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Crossfire
  5. {
  6.     static void Main()
  7.     {
  8.         int[] sizes = Console.ReadLine()
  9.             .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  10.             .Select(int.Parse)
  11.             .ToArray();
  12.  
  13.         int rowSize = sizes[0];
  14.         int colSize = sizes[1];
  15.  
  16.         int[][] matrix = FillMatrix(rowSize, colSize);
  17.  
  18.         string line;
  19.         while ((line = Console.ReadLine()) != "Nuke it from orbit")
  20.         {
  21.             int[] bombTokens = line
  22.                 .Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  23.                 .Select(int.Parse)
  24.                 .ToArray();
  25.  
  26.             int bombRow = bombTokens[0];
  27.             int bombCol = bombTokens[1];
  28.             int bombRadius = bombTokens[2];
  29.  
  30.             Explosion(ref matrix, bombRow, bombCol, bombRadius);
  31.         }
  32.  
  33.         foreach (var row in matrix)
  34.         {
  35.             Console.WriteLine(string.Join(" ", row.Where(n => n > 0)));
  36.         }
  37.     }
  38.  
  39.     private static void Explosion(ref int[][] matrix, int bombRow, int bombCol, int bombRadius)
  40.     {
  41.         //left and right
  42.         for (int rowIndex = bombRow - bombRadius; rowIndex <= bombRow + bombRadius; rowIndex++)
  43.         {
  44.             if (AreIndexesValid(rowIndex, bombCol, matrix))
  45.             {
  46.                 matrix[rowIndex][bombCol] = -1;
  47.             }
  48.         }
  49.  
  50.         //up and down
  51.         for (int colIndex = bombCol - bombRadius; colIndex <= bombCol + bombRadius; colIndex++)
  52.         {
  53.             if (AreIndexesValid(bombRow, colIndex, matrix))
  54.             {
  55.                 matrix[bombRow][colIndex] = -1;
  56.             }
  57.         }
  58.  
  59.         for (int rowIndex = 0; rowIndex < matrix.Length; rowIndex++)
  60.         {
  61.             if (matrix[rowIndex].Any(c => c == -1))
  62.             {
  63.                 matrix[rowIndex] = matrix[rowIndex].Where(n => n > 0).ToArray();
  64.             }
  65.  
  66.             if (matrix[rowIndex].Length < 1)
  67.             {
  68.                 matrix = matrix.Take(rowIndex).Concat(matrix.Skip(rowIndex + 1)).ToArray();
  69.                 rowIndex--;
  70.             }
  71.         }
  72.     }
  73.  
  74.     private static bool AreIndexesValid(int row, int col, int[][] matrix)
  75.     {
  76.         return row >= 0 && row < matrix.Length && col >= 0 && col < matrix[row].Length;
  77.     }
  78.  
  79.     private static int[][] FillMatrix(int rowSize, int colSize)
  80.     {
  81.         int[][] matrix = new int[rowSize][];
  82.  
  83.         int fillNumber = 1;
  84.  
  85.         for (int rowIndex = 0; rowIndex < rowSize; rowIndex++)
  86.         {
  87.             matrix[rowIndex] = new int[colSize];
  88.             for (int colIndex = 0; colIndex < colSize; colIndex++)
  89.             {
  90.                 matrix[rowIndex][colIndex] = fillNumber;
  91.                 fillNumber++;
  92.             }
  93.         }
  94.  
  95.         return matrix;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement