Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Crossfire
- {
- static void Main()
- {
- string input = Console.ReadLine();
- string[] matrixParams = input.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- int rows = int.Parse(matrixParams[0]);
- int cols = int.Parse(matrixParams[1]);
- int[,] matrix = new int[rows, cols];
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- matrix[i, j] = (j + 1) + (i*rows);
- }
- }
- int[][] resultMatrix = new int[rows][];
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- resultMatrix[i] = new int[matrix.GetLength(1)];
- for (int j = 0; j < matrix.GetLength(1); j++)
- {
- resultMatrix[i][j] = matrix[i, j];
- }
- }
- while (true)
- {
- string command = Console.ReadLine();
- if (command == "Nuke it from orbit")
- {
- break;
- }
- string[] commandParams = command.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- int centerRow = int.Parse(commandParams[0]);
- int centerCol = int.Parse(commandParams[1]);
- int radius = int.Parse(commandParams[2]);
- //Checking if the center coordinates are in the matrix and if so, putting the center of the cross
- if ((centerRow >= 0 && centerRow < resultMatrix.Length) &&
- (centerCol >= 0 && centerCol < resultMatrix[centerRow].Length))
- {
- //Setting the center of the cross
- resultMatrix[centerRow][centerCol] = 0;
- //Creating the cross based on the radius, starting from the center of the cross and going up, down, left and right
- for (int i = 0; i <= radius; i++)
- {
- //Going up from the center of the cross
- int cellUp = centerRow - i;
- if (cellUp >= 0)
- {
- if (centerCol < resultMatrix[cellUp].Length)
- {
- resultMatrix[cellUp][centerCol] = 0;
- }
- }
- //Going down from the center of the cross
- int cellDown = centerRow + i;
- if (cellDown < resultMatrix.Length)
- {
- if (centerCol < resultMatrix[cellDown].Length)
- {
- resultMatrix[cellDown][centerCol] = 0;
- }
- }
- //Going left from the center of the cross
- int cellLeft = centerCol - i;
- if (cellLeft >= 0)
- {
- resultMatrix[centerRow][cellLeft] = 0;
- }
- //Going right from the center of the cross
- int cellRight = centerCol + i;
- if (cellRight < resultMatrix[centerRow].Length)
- {
- resultMatrix[centerRow][cellRight] = 0;
- }
- }
- }
- //Check if the X coordinate of the center is outside of the matrix range, but the Y coordinates are in the matrix range
- if ((centerRow < 0 || centerRow >= resultMatrix.Length) && (centerCol >= 0 && centerCol < cols))
- {
- if (centerRow < 0)
- {
- if (radius > Math.Abs(centerRow))
- {
- int remainingRadius = radius - Math.Abs(centerRow);
- for (int i = 0; i < resultMatrix.Length; i++)
- {
- int rowDown = i;
- if (rowDown > remainingRadius - 1)
- {
- break;
- }
- if (centerCol < resultMatrix[rowDown].Length)
- {
- resultMatrix[rowDown][centerCol] = 0;
- }
- }
- }
- }
- if (centerRow >= resultMatrix.Length)
- {
- int remainingRadius = radius - (centerRow - (resultMatrix.Length - 1));
- for (int i = 0; i < remainingRadius; i++)
- {
- int rowUp = (resultMatrix.Length - 1) - i;
- if (rowUp < 0)
- {
- break;
- }
- if (centerCol < resultMatrix[rowUp].Length)
- {
- resultMatrix[rowUp][centerCol] = 0;
- }
- }
- }
- }
- //Check if the X coordinate of the center is in the matrix, but the Y coordinate is outside of the matrix range
- if ((centerRow >=0 && centerRow < resultMatrix.Length) && (centerCol < 0 || centerCol >= resultMatrix[centerRow].Length))
- {
- if (centerCol < 0)
- {
- if (radius > Math.Abs(centerCol))
- {
- int remainingRadius = radius - Math.Abs(centerCol);
- for (int i = 0; i < resultMatrix[centerRow].Length; i++)
- {
- int colRight = i;
- if (colRight > remainingRadius - 1)
- {
- break;
- }
- resultMatrix[centerRow][colRight] = 0;
- }
- }
- }
- if (centerCol >= resultMatrix[centerRow].Length)
- {
- int remainingRadius = radius - (centerCol - (resultMatrix[centerRow].Length - 1));
- for (int i = 0; i < remainingRadius; i++)
- {
- int colLeft = (resultMatrix[centerRow].Length - 1) - i;
- if (colLeft < 0)
- {
- break;
- }
- resultMatrix[centerRow][colLeft] = 0;
- }
- }
- }
- //Adding the remaining cells into a list
- List<List<int>> remainingCells = new List<List<int>>();
- for (int j = 0; j < resultMatrix.Length; j++)
- {
- remainingCells.Add(new List<int>());
- for (int k = 0; k < resultMatrix[j].Length; k++)
- {
- if (resultMatrix[j][k] != 0)
- {
- remainingCells[j].Add(resultMatrix[j][k]);
- }
- }
- }
- //Re-initializing the result matrix and filling it with the values in the list
- resultMatrix = new int[remainingCells.Count][];
- for (int j = 0; j < resultMatrix.Length; j++)
- {
- resultMatrix[j] = new int[remainingCells[j].Count];
- for (int k = 0; k < resultMatrix[j].Length; k++)
- {
- resultMatrix[j][k] = remainingCells[j][k];
- }
- }
- }
- for (int i = 0; i < resultMatrix.Length; i++)
- {
- for (int j = 0; j < resultMatrix[i].Length; j++)
- {
- Console.Write(resultMatrix[i][j] + " ");
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement