Advertisement
Guest User

Crossfire

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