desislava_topuzakova

Untitled

Sep 20th, 2021
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class demo {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. String dimensions = scanner.nextLine(); //"3 4" -> split -> ["3", "4"]
  10. int rows = Integer.parseInt(dimensions.split("\\s+")[0]);
  11. int cols = Integer.parseInt(dimensions.split("\\s+")[1]);
  12.  
  13. int [][] matrix = new int [rows][cols];
  14. fillMatrix(rows, cols, matrix);
  15.  
  16. String command = scanner.nextLine();
  17. while(!command.equals("Nuke it from orbit")) {
  18. //command = "3 3 2"
  19. int row = Integer.parseInt(command.split("\\s+")[0]);
  20. int col = Integer.parseInt(command.split("\\s+")[1]);
  21. int radius = Integer.parseInt(command.split("\\s+")[2]);
  22.  
  23. //махаме по редове -> започваме от: текущия ред - радиус до текущия ред + радиуса
  24. for (int rowToRemove = row - radius; rowToRemove <= row + radius ; rowToRemove++) {
  25. //проверка дали редът, който ще махна е в матрицата
  26. if (rowToRemove >= 0 && rowToRemove < rows) {
  27. matrix[rowToRemove][col] = 0;
  28. }
  29. }
  30.  
  31. //махаме по колони -> започваме от: текуща колона - радиус до текуща колона + радиуса
  32. for (int colToRemove = col - radius; colToRemove <= col + radius; colToRemove++) {
  33. //проверка дали колоната, който ще махна е в матрицата
  34. if (colToRemove >= 0 && colToRemove < cols) {
  35. matrix[row][colToRemove] = 0;
  36. }
  37. }
  38.  
  39.  
  40. removeAllZeroElements(matrix, rows);
  41.  
  42. command = scanner.nextLine();
  43. }
  44.  
  45. }
  46.  
  47. private static void removeAllZeroElements(int[][] matrix, int rows) {
  48. //TODO: премахваме всички 0
  49. //TODO: всички елементи в реда са 0 -> трябва да премахнем реда
  50. //1 2 3 4 5
  51. //6 7 8 0 10
  52. //11 12 13 0 15
  53. //16 0 0 0 0
  54. //21 22 23 0 25
  55. }
  56.  
  57. private static void fillMatrix(int rows, int cols, int[][] matrix) {
  58. //fillMatrix
  59. int startNumber = 1;
  60. for (int row = 0; row < rows; row++) {
  61. for (int col = 0; col < cols; col++) {
  62. matrix[row][col] = startNumber++;
  63. }
  64. }
  65. }
  66.  
  67.  
  68. public static void printMatrix (String [][] matrix, int rows, int cols) {
  69. for (int row = 0; row < rows; row++) {
  70. for (int col = 0; col < cols; col++) {
  71. System.out.print(matrix[row][col] + " ");
  72. }
  73. System.out.println();
  74. }
  75. }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment