John_IV

Untitled

Jan 21st, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. public class E07Crossfire {
  2.     public static void main(String[] args) throws IOException {
  3.         BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
  4.  
  5.         int[] info = Arrays.stream(rd.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  6.         List<List<Integer>> mat = new ArrayList<>(info[0]);
  7.         int n = 1;
  8.  
  9.         for (int i = 0; i < info[0]; i++) {
  10.             mat.add(new ArrayList<>());
  11.             for (int j = 0; j < info[1]; j++)
  12.                 mat.get(i).add(n++);
  13.         }
  14.  
  15.         String command;
  16.         while (!("Nuke it from orbit").equals(command = rd.readLine())) {
  17.             int[] input = Arrays.stream(command.split(" ")).mapToInt(Integer::parseInt).toArray();
  18.             int row = input[0];
  19.             int col = input[1];
  20.             int rad = input[2];
  21.  
  22.             for (int i = row - rad; i <= row + rad; i++) {
  23.  
  24.                 if (i == row)
  25.                     removeRow(mat, row, col, rad);
  26.                 else if (valid(mat, i, col))
  27.                     mat.get(i).remove(col);
  28.             }
  29.  
  30.             mat.removeIf(List::isEmpty);
  31.         }
  32.  
  33.         printMatrix(mat);
  34.     }
  35.  
  36.     private static void removeRow(List<List<Integer>> mat, int row, int col, int rad) {
  37.         for (int i = col + rad; i >= col - rad; i--)
  38.             if (valid(mat, row, i))
  39.                 mat.get(row).remove(i);
  40.     }
  41.  
  42.     private static void printMatrix(List<List<Integer>> mat) {
  43.         for (List<Integer> ints : mat)
  44.             if (!ints.isEmpty()) System.out.println(ints.toString().replaceAll("[\\[\\],]", ""));
  45.     }
  46.  
  47.     private static boolean valid(List<List<Integer>> mat, int row, int col) {
  48.         return 0 <= row && row < mat.size() && 0 <= col && col < mat.get(row).size();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment