Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MultidimensionalArrays.Exercise;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Crossfire {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[][] matrix = buildMatrix(scanner);
- String[][] nukedMatrix = nukeMatrix(matrix, scanner);
- printMatrix(nukedMatrix);
- }
- private static String[][] nukeMatrix(String[][] matrix, Scanner scanner) {
- String[][] newMatrix = matrix;
- String command;
- while (!"Nuke it from orbit".equals(command = scanner.nextLine())){
- int[] tokens = getTokens(command);
- int rowOfImpact = tokens[0];
- int colOfImpact = tokens[1];
- int radius = tokens[2];
- //delete top
- if (rowOfImpact != 0){
- int checked = 0;
- for (int row = rowOfImpact - 1; row >= 0; row--) {
- if (radius - checked > 0 && newMatrix[row].length -1 >= colOfImpact){
- newMatrix[row][colOfImpact] = ",";
- String temp = String.join(" ", newMatrix[row]).replace(",", "");
- newMatrix[row] = temp.split("\\s+");
- }
- checked++;
- }
- }
- //delete right
- if (colOfImpact != newMatrix[rowOfImpact].length - 1){
- int checked = 0;
- for (int col = colOfImpact + 1; col < newMatrix[rowOfImpact].length; col++) {
- if (radius - checked > 0){
- newMatrix[rowOfImpact][col] = ",";
- String temp = String.join(" ", newMatrix[rowOfImpact]).replace(",", "");
- newMatrix[rowOfImpact] = temp.split("\\s+");
- }
- checked++;
- }
- }
- //delete bottom
- if (rowOfImpact != newMatrix.length - 1){
- int checked = 0;
- for (int row = rowOfImpact + 1; row < newMatrix.length; row++) {
- if (radius - checked > 0 && newMatrix[row].length -1 >= colOfImpact){
- newMatrix[row][colOfImpact] = ",";
- String temp = String.join(" ", newMatrix[row]).replace(",", "");
- newMatrix[row] = temp.split("\\s+");
- }
- checked++;
- }
- }
- //delete left
- if (colOfImpact < newMatrix[rowOfImpact].length || colOfImpact - radius < newMatrix[rowOfImpact].length){
- int checked = 0;
- int tempCol = colOfImpact;
- if (colOfImpact >= newMatrix[rowOfImpact].length){
- while (tempCol >= newMatrix[rowOfImpact].length){
- tempCol--;
- }
- }
- for (int col = tempCol; col >= 0; col--) {
- if (radius - checked >= 0){
- newMatrix[rowOfImpact][col] = ",";
- String temp = String.join(" ", newMatrix[rowOfImpact]).replace(",", "");
- newMatrix[rowOfImpact] = temp.split("\\s+");
- }
- checked++;
- }
- }
- }
- return newMatrix;
- }
- private static void printMatrix(String[][] matrix) {
- for (int row = 0; row < matrix.length; row++) {
- for (int col = 0; col < matrix[row].length; col++) {
- System.out.printf("%s ", matrix[row][col]);
- }
- System.out.println();
- }
- }
- private static String[][] buildMatrix(Scanner scanner) {
- int[] tokens = getTokens(scanner.nextLine());
- int rows = tokens[0];
- int cols = tokens[1];
- String[][] matrix = new String[rows][cols];
- int counter = 0;
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < cols; col++) {
- if (counter > 0){
- matrix[row][col] = String.format("%d", (counter * cols) + col + 1);
- }else{
- matrix[row][col] = String.format("%d",col + 1);
- }
- }
- counter++;
- }
- return matrix;
- }
- private static int[] getTokens(String input) {
- return Arrays.stream(input.split("\\s+"))
- .mapToInt(Integer::parseInt)
- .toArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment