Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Crossfire
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] sizes = Console.ReadLine()
- .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int[][] jagged = new int[sizes[0]][];
- for (int i = 0; i < jagged.Length; i++)
- {
- jagged[i] = new int[sizes[1]];
- for (int j = 0; j < jagged[i].Length; j++)
- {
- jagged[i][j] = i * jagged[i].Length + j + 1;
- }
- }
- string command;
- while ((command = Console.ReadLine()) != "Nuke it from orbit")
- {
- int[] parameters = command
- .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int targetRow = parameters[0];
- int targetCol = parameters[1];
- int radius = parameters[2];
- // Place 0 to the target
- for (int i = Math.Max(0, targetRow - radius); i <= Math.Min(targetRow + radius, jagged.Length - 1); i++)
- {
- if (jagged[i].Length > targetCol && targetCol >= 0)
- {
- jagged[i][targetCol] = 0;
- }
- if (i == targetRow)
- {
- for (int j = Math.Max(0, targetCol - radius); j <= Math.Min(targetCol + radius, jagged[i].Length - 1); j++)
- {
- jagged[i][j] = 0;
- }
- }
- }
- // Swap 0 with number standig to the right
- for (int i = 0; i < jagged.Length; i++)
- {
- for (int j = 0; j < jagged[i].Length - 1; j++)
- {
- if (jagged[i][j] == 0)
- {
- if (j + 1 < jagged[i].Length)
- {
- int temp = jagged[i][j + 1];
- jagged[i][j] = temp;
- jagged[i][j + 1] = 0;
- }
- }
- }
- }
- // Remove Rows with 0's
- int check = -1;
- int indexToRemove = -1;
- for (int i = 0; i < jagged.Length; i++)
- {
- for (int j = 0; j < jagged[i].Length; j++)
- {
- if (jagged[i][j] == 0)
- {
- check = -1;
- }
- else
- {
- check = -2;
- break;
- }
- }
- if (check == -1)
- {
- indexToRemove = i;
- break;
- }
- }
- // Remove the row filled with 0's and create a new array
- if (check == -1 && indexToRemove >= 0)
- {
- int[][] arrayResult = jagged
- .Where((arr, i) => i != indexToRemove) //skip row - indexToRemove
- .Select(arr => arr).ToArray();
- jagged = arrayResult;
- }
- // Use GC if problem with memory limit test
- GC.Collect();
- }
- Print(jagged);
- }
- private static void Print(int[][] jagged)
- {
- for (int i = 0; i < jagged.Length; i++)
- {
- for (int j = 0; j < jagged[i].Length; j++)
- {
- if (jagged[i][j] == 0)
- {
- continue;
- }
- else
- {
- Console.Write(jagged[i][j] + " ");
- }
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment