murkata86

Proble 19 - Crossfire

May 29th, 2016
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Crossfire
  5. {
  6.     static void Main()
  7.     {
  8.         string input = Console.ReadLine();
  9.  
  10.         string[] matrixParams = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  11.  
  12.         int rows = int.Parse(matrixParams[0]);
  13.         int cols = int.Parse(matrixParams[1]);
  14.  
  15.         int[, ] matrix = new int[rows, cols];
  16.  
  17.         for (int i = 0; i < matrix.GetLength(0); i++)
  18.         {
  19.             for (int j = 0; j < matrix.GetLength(1); j++)
  20.             {
  21.                 matrix[i, j] = (j + 1) + (i * rows);
  22.             }
  23.         }
  24.  
  25.         int[][] resultMatrix = new int[rows][];
  26.  
  27.         for (int i = 0; i < matrix.GetLength(0); i++)
  28.         {
  29.             resultMatrix[i] = new int[matrix.GetLength(1)];
  30.             for (int j = 0; j < matrix.GetLength(1); j++)
  31.             {
  32.                 resultMatrix[i][j] = matrix[i, j];
  33.             }
  34.         }
  35.  
  36.         while (true)
  37.         {
  38.             string command = Console.ReadLine();
  39.  
  40.             if (command == "Nuke it from orbit")
  41.             {
  42.                 break;
  43.             }
  44.  
  45.             string[] commandParams = command.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  46.  
  47.             int centerRow = Math.Abs(int.Parse(commandParams[0]));
  48.             int centerCol = Math.Abs(int.Parse(commandParams[1]));
  49.             int radius = int.Parse(commandParams[2]);
  50.  
  51.             //Checking if the center coordinates are in the matrix and if so, putting the center of the cross
  52.  
  53.             if ((centerRow >= 0 && centerRow < resultMatrix.Length) && (centerCol >= 0 && centerCol < resultMatrix[centerRow].Length))
  54.             {
  55.                 //Setting the center of the cross
  56.                 resultMatrix[centerRow][centerCol] = 0;
  57.  
  58.                 //Creating the cross based on the radius, starting from the center of the cross and going up, down, left and right
  59.  
  60.                 for (int i = 0; i <= radius; i++)
  61.                 {
  62.                     //Going up from the center of the cross
  63.                     int cellUp = centerRow - i;
  64.  
  65.                     if (cellUp >= 0)
  66.                     {
  67.                         if (centerCol < resultMatrix[cellUp].Length)
  68.                         {
  69.                             resultMatrix[cellUp][centerCol] = 0;
  70.                         }
  71.                     }
  72.  
  73.                     //Going down from the center of the cross
  74.                     int cellDown = centerRow + i;
  75.  
  76.                     if (cellDown < resultMatrix.Length)
  77.                     {
  78.                         if (centerCol < resultMatrix[cellDown].Length)
  79.                         {
  80.                             resultMatrix[cellDown][centerCol] = 0;
  81.                         }
  82.                     }
  83.  
  84.                     //Going left from the center of the cross
  85.                     int cellLeft = centerCol - i;
  86.  
  87.                     if (cellLeft >= 0)
  88.                     {
  89.                         resultMatrix[centerRow][cellLeft] = 0;
  90.                     }
  91.  
  92.                     //Going right from the center of the cross
  93.                     int cellRight = centerCol + i;
  94.  
  95.                     if (cellRight < resultMatrix[centerRow].Length)
  96.                     {
  97.                         resultMatrix[centerRow][cellRight] = 0;
  98.                     }                  
  99.                 }
  100.  
  101.                 //Adding the remaining cells into a list
  102.                 List<List<int>> remainingCells = new List<List<int>>();
  103.  
  104.                 for (int j = 0; j < resultMatrix.Length; j++)
  105.                 {
  106.                     remainingCells.Add(new List<int>());
  107.                     for (int k = 0; k < resultMatrix[j].Length; k++)
  108.                     {
  109.                         if (resultMatrix[j][k] != 0)
  110.                         {
  111.                             remainingCells[j].Add(resultMatrix[j][k]);
  112.                         }
  113.                     }
  114.                 }
  115.  
  116.                 //Re-initializing the result matrix and filling it with the values in the list
  117.                 resultMatrix = new int[remainingCells.Count][];
  118.  
  119.                 for (int j = 0; j < resultMatrix.Length; j++)
  120.                 {
  121.                     resultMatrix[j] = new int[remainingCells[j].Count];
  122.                     for (int k = 0; k < resultMatrix[j].Length; k++)
  123.                     {
  124.                         resultMatrix[j][k] = remainingCells[j][k];
  125.                     }
  126.                 }
  127.             }          
  128.         }
  129.  
  130.         for (int i = 0; i < resultMatrix.Length; i++)
  131.         {
  132.             for (int j = 0; j < resultMatrix[i].Length; j++)
  133.             {
  134.                 Console.Write(resultMatrix[i][j] + " ");
  135.             }
  136.             Console.WriteLine();
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment