Advertisement
nikeza

7.Crossfire/List<List<Integer>> matrix

Sep 25th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import org.w3c.dom.ls.LSOutput;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class matrix1_Exercises_7Crossfire {
  9.     public static void main(String[] args) throws IOException {
  10.         //Scanner scanner = new Scanner(System.in);
  11.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  12.         int[] input = Arrays.stream(bf.readLine().split("\\s+"))
  13.                 .mapToInt(Integer::parseInt).toArray();
  14.         int r = input[0];
  15.         int c = input[1];
  16.  
  17.         List<List<Integer>> matrix = new ArrayList<>();
  18.         fillMatrix(matrix, r, c);
  19.  
  20.         String command = bf.readLine();
  21.         while (!command.equals("Nuke it from orbit")) {
  22.             int[] tokens = Arrays.stream(command.split("\\s+"))
  23.                     .mapToInt(Integer::parseInt).toArray();
  24.             int row = tokens[0];
  25.             int col = tokens[1];
  26.             int rad = tokens[2];
  27.  
  28.             checkVerticalBomb(matrix, row, col, rad);
  29.             checkHorizontalBomb(matrix, row, col, rad);
  30.  
  31.             matrix.removeIf(List::isEmpty);
  32.  
  33.             command = bf.readLine();
  34.         }
  35.  
  36.         printMatrix(matrix);
  37.  
  38.       //===
  39.  
  40.     }
  41.  
  42.     private static void printMatrix(List<List<Integer>> matrix) {
  43.         for (List<Integer> row : matrix) {
  44.             for (Integer col : row) {
  45.                 System.out.print(col+" ");
  46.             }
  47.             System.out.println();
  48.         }
  49.  
  50.     }
  51.  
  52.     private static void checkVerticalBomb(List<List<Integer>> matrix, int row, int col, int rad) {
  53.         for (int i = row - rad; i <= row + rad; i++) {
  54.             if (isValidRange(matrix, i, col) && i != row) {
  55.                 matrix.get(i).remove(col);
  56.             }
  57.         }
  58.     }
  59.  
  60.     private static void checkHorizontalBomb(List<List<Integer>> matrix, int row, int col, int rad) {
  61.         for (int i = col + rad; i >=col - rad; i--) {
  62.             if (isValidRange(matrix, row, i)) {
  63.                 matrix.get(row).remove(i);
  64.                
  65.             }
  66.         }
  67.     }
  68.  
  69.     private static boolean isValidRange(List<List<Integer>> matrix, int row, int col) {
  70.         return 0 <= row && row < matrix.size() && 0 <= col && col < matrix.get(row).size();
  71.     }
  72.  
  73.     private static void fillMatrix(List<List<Integer>> matrix, int r, int c) {
  74.         int index = 1;
  75.         for (int i = 0; i < r; i++) {
  76.             matrix.add(new ArrayList<>());
  77.             for (int j = 0; j < c; j++) {
  78.                 matrix.get(i).add(index++);
  79.             }
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement