Advertisement
murkata86

Untitled

May 30th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.89 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 = int.Parse(commandParams[0]);
  48.             int centerCol = 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) &&
  54.                 (centerCol >= 0 && centerCol < resultMatrix[centerRow].Length))
  55.             {
  56.                 //Setting the center of the cross
  57.                 resultMatrix[centerRow][centerCol] = 0;
  58.  
  59.                 //Creating the cross based on the radius, starting from the center of the cross and going up, down, left and right
  60.  
  61.                 for (int i = 0; i <= radius; i++)
  62.                 {
  63.                     //Going up from the center of the cross
  64.                     int cellUp = centerRow - i;
  65.  
  66.                     if (cellUp >= 0)
  67.                     {
  68.                         if (centerCol < resultMatrix[cellUp].Length)
  69.                         {
  70.                             resultMatrix[cellUp][centerCol] = 0;
  71.                         }
  72.                     }
  73.  
  74.                     //Going down from the center of the cross
  75.                     int cellDown = centerRow + i;
  76.  
  77.                     if (cellDown < resultMatrix.Length)
  78.                     {
  79.                         if (centerCol < resultMatrix[cellDown].Length)
  80.                         {
  81.                             resultMatrix[cellDown][centerCol] = 0;
  82.                         }
  83.                     }
  84.  
  85.                     //Going left from the center of the cross
  86.                     int cellLeft = centerCol - i;
  87.  
  88.                     if (cellLeft >= 0)
  89.                     {
  90.                         resultMatrix[centerRow][cellLeft] = 0;
  91.                     }
  92.  
  93.                     //Going right from the center of the cross
  94.                     int cellRight = centerCol + i;
  95.  
  96.                     if (cellRight < resultMatrix[centerRow].Length)
  97.                     {
  98.                         resultMatrix[centerRow][cellRight] = 0;
  99.                     }
  100.                 }
  101.             }
  102.             //Check if the X coordinate of the center is outside of the matrix range, but the Y coordinates are in the matrix range
  103.             if ((centerRow < 0 || centerRow >= resultMatrix.Length) && (centerCol >= 0 && centerCol < cols))
  104.             {
  105.                 if (centerRow < 0)
  106.                 {
  107.                     if (radius > Math.Abs(centerRow))
  108.                     {
  109.                         int remainingRadius = radius - Math.Abs(centerRow);
  110.  
  111.                         for (int i = 0; i < resultMatrix.Length; i++)
  112.                         {
  113.                             int rowDown = i;
  114.  
  115.                             if (rowDown > remainingRadius - 1)
  116.                             {
  117.                                 break;
  118.                             }
  119.  
  120.                             if (centerCol < resultMatrix[rowDown].Length)
  121.                             {
  122.                                 resultMatrix[rowDown][centerCol] = 0;
  123.                             }
  124.                         }
  125.                     }
  126.                 }
  127.  
  128.                 if (centerRow >= resultMatrix.Length)
  129.                 {
  130.                     int remainingRadius = radius - (centerRow - (resultMatrix.Length - 1));
  131.  
  132.                     for (int i = 0; i < remainingRadius; i++)
  133.                     {
  134.                         int rowUp = (resultMatrix.Length - 1) - i;
  135.  
  136.                         if (rowUp < 0)
  137.                         {
  138.                             break;
  139.                         }
  140.  
  141.                         if (centerCol < resultMatrix[rowUp].Length)
  142.                         {
  143.                             resultMatrix[rowUp][centerCol] = 0;
  144.                         }
  145.                     }
  146.                 }
  147.             }
  148.  
  149.             //Check if the X coordinate of the center is in the matrix, but the Y coordinate is outside of the matrix range
  150.             if ((centerRow >=0 && centerRow < resultMatrix.Length) && (centerCol < 0 || centerCol >= resultMatrix[centerRow].Length))
  151.             {
  152.                 if (centerCol < 0)
  153.                 {
  154.                     if (radius > Math.Abs(centerCol))
  155.                     {
  156.                         int remainingRadius = radius - Math.Abs(centerCol);
  157.  
  158.                         for (int i = 0; i < resultMatrix[centerRow].Length; i++)
  159.                         {
  160.                             int colRight = i;
  161.  
  162.                             if (colRight > remainingRadius - 1)
  163.                             {
  164.                                 break;
  165.                             }
  166.  
  167.                             resultMatrix[centerRow][colRight] = 0;
  168.                         }
  169.                     }
  170.                 }
  171.  
  172.                 if (centerCol >= resultMatrix[centerRow].Length)
  173.                 {
  174.                     int remainingRadius = radius - (centerCol - (resultMatrix[centerRow].Length - 1));
  175.  
  176.                     for (int i = 0; i < remainingRadius; i++)
  177.                     {
  178.                         int colLeft = (resultMatrix[centerRow].Length - 1) - i;
  179.  
  180.                         if (colLeft < 0)
  181.                         {
  182.                             break;
  183.                         }
  184.  
  185.                         resultMatrix[centerRow][colLeft] = 0;
  186.                     }
  187.  
  188.                    
  189.                 }
  190.             }
  191.  
  192.             //Adding the remaining cells into a list
  193.             List<List<int>> remainingCells = new List<List<int>>();
  194.  
  195.             for (int j = 0; j < resultMatrix.Length; j++)
  196.             {
  197.                 remainingCells.Add(new List<int>());
  198.                 for (int k = 0; k < resultMatrix[j].Length; k++)
  199.                 {
  200.                     if (resultMatrix[j][k] != 0)
  201.                     {
  202.                         remainingCells[j].Add(resultMatrix[j][k]);
  203.                     }
  204.                 }
  205.             }
  206.  
  207.             //Re-initializing the result matrix and filling it with the values in the list
  208.             resultMatrix = new int[remainingCells.Count][];
  209.  
  210.             for (int j = 0; j < resultMatrix.Length; j++)
  211.             {
  212.                 resultMatrix[j] = new int[remainingCells[j].Count];
  213.                 for (int k = 0; k < resultMatrix[j].Length; k++)
  214.                 {
  215.                     resultMatrix[j][k] = remainingCells[j][k];
  216.                 }
  217.             }
  218.         }
  219.  
  220.         for (int i = 0; i < resultMatrix.Length; i++)
  221.         {
  222.             for (int j = 0; j < resultMatrix[i].Length; j++)
  223.             {
  224.                 Console.Write(resultMatrix[i][j] + " ");
  225.             }
  226.             Console.WriteLine();
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement