desislava_topuzakova

Untitled

May 25th, 2021
1,590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. //вход
  7. int[] dimensions = Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  8. int rows = dimensions[0];
  9. int cols = dimensions[1];
  10.  
  11.  
  12. List<List<Integer>> matrix = new ArrayList<>();
  13. fillMatrix(matrix, rows, cols);
  14.  
  15. String command = scanner.nextLine();
  16. while (!command.equals("Nuke it from orbit")) {
  17. String[] tokens = command.split("\\s+"); // 3 4 1
  18. int row = Integer.parseInt(tokens[0]);
  19. int col = Integer.parseInt(tokens[1]); //индексът на елемента в листа
  20. int radius = Integer.parseInt(tokens[2]);
  21.  
  22. //destroy up, down
  23. for (int currentRow = row - radius; currentRow <= row + radius ; currentRow++) {
  24. if(isInMatrix(currentRow, col, matrix)) { //валидираме реда от който ще вземем
  25. matrix.get(currentRow).remove(col);
  26. }
  27. }
  28.  
  29. //destroy left. right
  30. for (int currentColumn = col + radius; currentColumn >- col - radius; currentColumn--) {
  31. if(isInMatrix(row, currentColumn, matrix)) { //валидираме колоната от който ще вземем
  32. matrix.get(row).remove(currentColumn);
  33. }
  34. }
  35.  
  36. matrix.removeIf(List::isEmpty);
  37.  
  38.  
  39. command = scanner.nextLine();
  40.  
  41. }
  42.  
  43. printMatrix(matrix);
  44. }
  45.  
  46. private static boolean isInMatrix(int row, int col, List<List<Integer>> matrix) {
  47. return row >= 0 && row < matrix.size() && col >= 0 && col < matrix.get(row).size();
  48. }
  49.  
  50.  
  51. //fill matrix
  52. private static void fillMatrix(List<List<Integer>> matrix, int rows, int cols) {
  53. int number = 1;
  54. for (int row = 0; row < rows; row++) {
  55. matrix.add(new ArrayList<>());
  56. for (int col = 0; col < cols; col++) {
  57. matrix.get(row).add(number++);
  58. }
  59. }
  60. }
  61.  
  62. //print matrix
  63. public static void printMatrix(List<List<Integer>> matrix) {
  64. for (List<Integer> row : matrix) {
  65. for (int element : row ) {
  66. System.out.print(element + " ");
  67. }
  68. System.out.println();
  69. }
  70. }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment