kalitarix

Crossfire

Sep 27th, 2018
3,651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Crossfire
  5. {
  6.     class Crossfire
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] dimensions = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  11.  
  12.             int[][] matrix = new int[dimensions[0]][];
  13.  
  14.             int number = 1;
  15.  
  16.             for (int row = 0; row < matrix.Length; row++)
  17.             {
  18.                 matrix[row] = new int[dimensions[1]];
  19.  
  20.                 for (int col = 0; col < dimensions[1]; col++)
  21.                 {
  22.                     matrix[row][col] = number++;
  23.                 }
  24.             }
  25.  
  26.             string input = Console.ReadLine();
  27.  
  28.             while (true)
  29.             {
  30.                 if (input == "Nuke it from orbit")
  31.                 {
  32.                     break;
  33.                 }
  34.  
  35.                 int[] parameters = input.Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  36.                 int targetRow = parameters[0];
  37.                 int targetCol = parameters[1];
  38.                 int range = parameters[2];
  39.                
  40.                 for (int row = targetRow - range; row <= targetRow + range; row++)
  41.                 {
  42.                     if (indicesAreInRange(row, targetCol, matrix))
  43.                     {
  44.                         matrix[row][targetCol] = 0;
  45.                     }
  46.                 }
  47.  
  48.                 for (int col = targetCol - range; col <= targetCol + range; col++)
  49.                 {
  50.                     if (indicesAreInRange(targetRow, col, matrix))
  51.                     {
  52.                         matrix[targetRow][col] = 0;
  53.                     }
  54.                 }
  55.  
  56.                 for (int index = 0; index < matrix.Length; index++)
  57.                 {
  58.                     if (matrix[index].Any(e => e == 0))
  59.                     {
  60.                         matrix[index] = matrix[index].Where(e => e != 0).ToArray();
  61.                     }
  62.                 }
  63.  
  64.                 matrix = matrix.Where(arr => arr.Count() != 0).ToArray();
  65.  
  66.                 input = Console.ReadLine();
  67.             }
  68.  
  69.             foreach (int[] array in matrix)
  70.             {
  71.                 Console.WriteLine(string.Join(" ", array));
  72.             }
  73.         }
  74.  
  75.         private static bool indicesAreInRange(int row, int col, int[][] matrix)
  76.         {
  77.             return row >= 0 && row < matrix.Length && col >= 0 && col < matrix[row].Length;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment