Mirineo

07. Crossfire

Jan 26th, 2021
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. package MultidimensionalArrays.Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Crossfire {
  7. public static void main(String[] args) {
  8.  
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. String[][] matrix = buildMatrix(scanner);
  12. String[][] nukedMatrix = nukeMatrix(matrix, scanner);
  13. printMatrix(nukedMatrix);
  14.  
  15.  
  16. }
  17.  
  18. private static String[][] nukeMatrix(String[][] matrix, Scanner scanner) {
  19. String[][] newMatrix = matrix;
  20. String command;
  21. while (!"Nuke it from orbit".equals(command = scanner.nextLine())){
  22. int[] tokens = getTokens(command);
  23. int rowOfImpact = tokens[0];
  24. int colOfImpact = tokens[1];
  25. int radius = tokens[2];
  26.  
  27. //delete top
  28. if (rowOfImpact != 0){
  29. int checked = 0;
  30. for (int row = rowOfImpact - 1; row >= 0; row--) {
  31. if (radius - checked > 0 && newMatrix[row].length -1 >= colOfImpact){
  32. newMatrix[row][colOfImpact] = ",";
  33. String temp = String.join(" ", newMatrix[row]).replace(",", "");
  34. newMatrix[row] = temp.split("\\s+");
  35. }
  36. checked++;
  37. }
  38. }
  39. //delete right
  40. if (colOfImpact != newMatrix[rowOfImpact].length - 1){
  41. int checked = 0;
  42. for (int col = colOfImpact + 1; col < newMatrix[rowOfImpact].length; col++) {
  43. if (radius - checked > 0){
  44. newMatrix[rowOfImpact][col] = ",";
  45. String temp = String.join(" ", newMatrix[rowOfImpact]).replace(",", "");
  46. newMatrix[rowOfImpact] = temp.split("\\s+");
  47. }
  48. checked++;
  49. }
  50. }
  51.  
  52. //delete bottom
  53. if (rowOfImpact != newMatrix.length - 1){
  54. int checked = 0;
  55. for (int row = rowOfImpact + 1; row < newMatrix.length; row++) {
  56. if (radius - checked > 0 && newMatrix[row].length -1 >= colOfImpact){
  57. newMatrix[row][colOfImpact] = ",";
  58. String temp = String.join(" ", newMatrix[row]).replace(",", "");
  59. newMatrix[row] = temp.split("\\s+");
  60. }
  61. checked++;
  62. }
  63. }
  64.  
  65. //delete left
  66. if (colOfImpact < newMatrix[rowOfImpact].length || colOfImpact - radius < newMatrix[rowOfImpact].length){
  67. int checked = 0;
  68. int tempCol = colOfImpact;
  69. if (colOfImpact >= newMatrix[rowOfImpact].length){
  70. while (tempCol >= newMatrix[rowOfImpact].length){
  71. tempCol--;
  72. }
  73. }
  74.  
  75. for (int col = tempCol; col >= 0; col--) {
  76. if (radius - checked >= 0){
  77. newMatrix[rowOfImpact][col] = ",";
  78. String temp = String.join(" ", newMatrix[rowOfImpact]).replace(",", "");
  79. newMatrix[rowOfImpact] = temp.split("\\s+");
  80. }
  81. checked++;
  82. }
  83. }
  84.  
  85. }
  86. return newMatrix;
  87. }
  88.  
  89. private static void printMatrix(String[][] matrix) {
  90. for (int row = 0; row < matrix.length; row++) {
  91. for (int col = 0; col < matrix[row].length; col++) {
  92. System.out.printf("%s ", matrix[row][col]);
  93. }
  94. System.out.println();
  95. }
  96. }
  97.  
  98. private static String[][] buildMatrix(Scanner scanner) {
  99. int[] tokens = getTokens(scanner.nextLine());
  100.  
  101. int rows = tokens[0];
  102. int cols = tokens[1];
  103.  
  104. String[][] matrix = new String[rows][cols];
  105.  
  106. int counter = 0;
  107. for (int row = 0; row < rows; row++) {
  108. for (int col = 0; col < cols; col++) {
  109. if (counter > 0){
  110. matrix[row][col] = String.format("%d", (counter * cols) + col + 1);
  111. }else{
  112. matrix[row][col] = String.format("%d",col + 1);
  113. }
  114. }
  115. counter++;
  116. }
  117.  
  118. return matrix;
  119. }
  120.  
  121. private static int[] getTokens(String input) {
  122. return Arrays.stream(input.split("\\s+"))
  123. .mapToInt(Integer::parseInt)
  124. .toArray();
  125. }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment