Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.w3c.dom.ls.LSOutput;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class matrix1_Exercises_7Crossfire {
- public static void main(String[] args) throws IOException {
- //Scanner scanner = new Scanner(System.in);
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- int[] input = Arrays.stream(bf.readLine().split("\\s+"))
- .mapToInt(Integer::parseInt).toArray();
- int r = input[0];
- int c = input[1];
- List<List<Integer>> matrix = new ArrayList<>();
- fillMatrix(matrix, r, c);
- String command = bf.readLine();
- while (!command.equals("Nuke it from orbit")) {
- int[] tokens = Arrays.stream(command.split("\\s+"))
- .mapToInt(Integer::parseInt).toArray();
- int row = tokens[0];
- int col = tokens[1];
- int rad = tokens[2];
- checkVerticalBomb(matrix, row, col, rad);
- checkHorizontalBomb(matrix, row, col, rad);
- matrix.removeIf(List::isEmpty);
- command = bf.readLine();
- }
- printMatrix(matrix);
- //===
- }
- private static void printMatrix(List<List<Integer>> matrix) {
- for (List<Integer> row : matrix) {
- for (Integer col : row) {
- System.out.print(col+" ");
- }
- System.out.println();
- }
- }
- private static void checkVerticalBomb(List<List<Integer>> matrix, int row, int col, int rad) {
- for (int i = row - rad; i <= row + rad; i++) {
- if (isValidRange(matrix, i, col) && i != row) {
- matrix.get(i).remove(col);
- }
- }
- }
- private static void checkHorizontalBomb(List<List<Integer>> matrix, int row, int col, int rad) {
- for (int i = col + rad; i >=col - rad; i--) {
- if (isValidRange(matrix, row, i)) {
- matrix.get(row).remove(i);
- }
- }
- }
- private static boolean isValidRange(List<List<Integer>> matrix, int row, int col) {
- return 0 <= row && row < matrix.size() && 0 <= col && col < matrix.get(row).size();
- }
- private static void fillMatrix(List<List<Integer>> matrix, int r, int c) {
- int index = 1;
- for (int i = 0; i < r; i++) {
- matrix.add(new ArrayList<>());
- for (int j = 0; j < c; j++) {
- matrix.get(i).add(index++);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement