morgan73

CrossFire

Jun 5th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace _09.Crossfire
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     class Program
  8.     {
  9.         public static int Rows;
  10.         public static int Cols;
  11.         public static List<List<long>> Matrix;
  12.  
  13.         public static void Main()
  14.         {
  15.             int[] dimentions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  16.             Rows = dimentions[0];
  17.             Cols = dimentions[1];
  18.             string input = Console.ReadLine();
  19.             FillMatrix();
  20.             while (input != "Nuke it from orbit")
  21.             {
  22.                 var command = input.Split();
  23.                 int cellRow = int.Parse(command[0]);
  24.                 int cellCol = int.Parse(command[1]);
  25.                 int radius = int.Parse(command[2]);
  26.                 Nuke(cellRow, cellCol, radius);
  27.                 ReorderMatrix();
  28.                 input = Console.ReadLine();
  29.             }
  30.  
  31.             PrintMatrix();
  32.         }
  33.  
  34.         public static void FillMatrix()
  35.         {
  36.             Matrix = new List<List<long>>();
  37.             int count = 1;
  38.             for (int i = 0; i < Rows; i++)
  39.             {
  40.                 Matrix.Add(new List<long>());
  41.                 for (int k = 0; k < Cols; k++)
  42.                 {
  43.                     Matrix[i].Add(count);
  44.                     count++;
  45.                 }
  46.             }
  47.         }
  48.  
  49.         public static void Nuke(int row, int col, int radius) {
  50.             for (int i = col - radius; i <= col + radius; i++)
  51.             {
  52.                 if (isInMatrix(row, i))
  53.                 {
  54.                     Matrix[row][i] = 0;
  55.                 }
  56.             }
  57.  
  58.             for (int i = row - radius; i <= row + radius; i++)
  59.             {
  60.                 if (isInMatrix(i, col))
  61.                 {
  62.                     Matrix[i][col] = 0;
  63.                 }
  64.             }
  65.  
  66.         }
  67.  
  68.         public static bool isInMatrix(int row, int col)
  69.         {
  70.             int matrixRows = Matrix.Count;
  71.             if (row < 0 || row >= matrixRows)
  72.             {
  73.                 return false;
  74.             }
  75.             else
  76.             {
  77.                 int matrixCols = Matrix[row].Count;
  78.                 if (col < 0 || col >= matrixCols)
  79.                 {
  80.                     return false;
  81.                 }
  82.             }
  83.  
  84.             return true;
  85.         }
  86.  
  87.         public static void ReorderMatrix()
  88.         {
  89.             for (int i = 0; i < Matrix.Count; i++)
  90.             {
  91.                 for (int k = Matrix[i].Count - 1; k >= 0; k--)
  92.                 {
  93.                     if (Matrix[i][k] == 0)
  94.                     {
  95.                         Matrix[i].RemoveAt(k);
  96.                     }
  97.                 }
  98.  
  99.                 if (Matrix[i].Count == 0)
  100.                 {
  101.                     Matrix.RemoveAt(i);
  102.                     i--;
  103.                 }
  104.             }
  105.         }
  106.  
  107.         public static void PrintMatrix()
  108.         {
  109.             if (Matrix.Count != 0)
  110.             {
  111.                 for (int i = 0; i < Matrix.Count; i++)
  112.                 {
  113.                     var row = string.Join(" ", Matrix[i]);
  114.                     if (row.Length == 0)
  115.                     {
  116.                         continue;
  117.                     }
  118.  
  119.                     Console.WriteLine(row);
  120.                 }
  121.             }
  122.         }
  123.     }
  124. }
Add Comment
Please, Sign In to add comment