Advertisement
Guest User

Crossfire

a guest
Apr 28th, 2017
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         int[] dimensions = StringToIntArray(Console.ReadLine());
  11.         int rows = dimensions[0];
  12.         int cols = dimensions[1];
  13.         List<List<Cell>> matrix = InitializeMatrix(rows, cols);
  14.         string input = Console.ReadLine();
  15.         while (!input.Equals("Nuke it from orbit"))
  16.         {
  17.             int[] destroyData = StringToIntArray(input);
  18.             int[] coords = new int[] { destroyData[0], destroyData[1] };
  19.             int radius = destroyData[2];
  20.  
  21.             for (int i = 0; i < matrix.Count; i++)
  22.             {
  23.                 for (int j = 0; j < matrix[i].Count; j++)
  24.                 {
  25.                     var cell = matrix[i][j];
  26.                     int distance = 0;
  27.                     bool eligibleForDestruction = cell.Row == coords[0] || cell.Col == coords[1];//same row or col
  28.                     if (eligibleForDestruction)
  29.                     {
  30.                         if (cell.Row == coords[0])
  31.                         {
  32.                             distance = Math.Abs(cell.Col - coords[1]);
  33.                         }
  34.                         else // j == coords[1]
  35.                         {
  36.                             distance = Math.Abs(cell.Row - coords[0]);
  37.                         }
  38.  
  39.                         if (distance <= radius)
  40.                         {
  41.                             matrix[i].Remove(cell);
  42.                             if (cell.Row == coords[0]) // next cell remains at same index
  43.                             {
  44.                                 j--;
  45.                             }
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             ClearUp(matrix);
  52.             input = Console.ReadLine();
  53.         }
  54.  
  55.         PrintMatrix(matrix);
  56.     }
  57.  
  58.     private static int[] StringToIntArray(string text)
  59.     {
  60.         return text
  61.               .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  62.               .Select(int.Parse)
  63.               .ToArray();
  64.     }
  65.  
  66.     private static List<List<Cell>> InitializeMatrix(int rows, int cols)
  67.     {
  68.         var matrix = new List<List<Cell>>(rows);
  69.         for (int i = 0; i < rows; i++)
  70.         {
  71.             matrix.Add(new List<Cell>(cols));
  72.             for (int j = 0; j < cols; j++)
  73.             {
  74.                 matrix[i].Add(new Cell(i, j, i * cols + j + 1));
  75.             }
  76.         }
  77.  
  78.         return matrix;
  79.     }
  80.    
  81.     private static void ClearUp(List<List<Cell>> matrix) // remove empty lines and update cell coordinates
  82.     {
  83.         int row = 0;
  84.         while (row < matrix.Count)
  85.         {
  86.             if (matrix[row].Count == 0)
  87.             {
  88.                 matrix.RemoveAt(row);
  89.             }
  90.             else
  91.             {
  92.                 int col = 0;
  93.                 while (col < matrix[row].Count)
  94.                 {
  95.                     matrix[row][col].Row = row;
  96.                     matrix[row][col].Col = col++;
  97.                 }
  98.                 row++;
  99.             }
  100.         }
  101.     }
  102.  
  103.     private static void PrintMatrix(List<List<Cell>> matrix)
  104.     {
  105.         foreach (var row in matrix)
  106.         {
  107.             foreach (var cell in row)
  108.             {
  109.                 Console.Write($"{cell.Value} ");
  110.             }
  111.             Console.WriteLine();
  112.         }
  113.     }
  114. }
  115.  
  116. class Cell
  117. {
  118.     public int Row { get; set; }
  119.     public int Col { get; set; }
  120.     public int Value { get; set; }
  121.  
  122.     public Cell(int row, int col, int value)
  123.     {
  124.         this.Row = row;
  125.         this.Col = col;
  126.         this.Value = value;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement