Advertisement
CuBG

Untitled

May 17th, 2023
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | Software | 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 Crossfire {
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         int[] range = Arrays.stream(sc.nextLine().split("\\s+"))
  11.                 .mapToInt(Integer::parseInt)
  12.                 .toArray();
  13.  
  14.         int rows = range[0];
  15.         int cols = range[1];
  16.  
  17.         List<List<Integer>> matrix = new ArrayList<>();
  18.         fillMatrix(matrix, rows, cols);
  19.  
  20.         String command = sc.nextLine();
  21.         while (!command.equals("Nuke it from orbit")) {
  22.             String[] tokens = command.split("\\s+"); // 3 4 1
  23.             int row = Integer.parseInt(tokens[0]);
  24.             int col = Integer.parseInt(tokens[1]); //индексът на елемента в листа
  25.             int radius = Integer.parseInt(tokens[2]);
  26.  
  27.             for (int currentRow = row - radius; currentRow <= row + radius; currentRow++) {
  28.                 if (isInMatrix(currentRow, col, matrix)) {
  29.                     matrix.get(currentRow).remove(col);
  30.                 }
  31.             }
  32.  
  33.             for (int currentCol = col + radius; currentCol >= col - radius; currentCol--) {
  34.                 if (isInMatrix(row, currentCol, matrix)) {
  35.                     matrix.get(row).remove(currentCol);
  36.                 }
  37.             }
  38.  
  39.            matrix.removeIf(List::isEmpty);
  40.  
  41.             command = sc.nextLine();
  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.     private static void fillMatrix(List<List<Integer>> matrix, int rows, int cols) {
  51.         int number = 1;
  52.         for (int row = 0; row < rows; row++) {
  53.             matrix.add(new ArrayList<>());
  54.             for (int col = 0; col < cols; col++) {
  55.                 matrix.get(row).add(number++);
  56.             }
  57.         }
  58.     }
  59.     public static void printMatrix(List<List<Integer>> matrix) {
  60.         matrix.forEach(row -> {
  61.             for (Integer element : row) {
  62.                 System.out.print(element + " ");
  63.             }
  64.             System.out.println();
  65.         });
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement