Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class TaskFour {
  6.     public static void main(String[] args) {
  7.         TaskFour instance = new TaskFour();
  8.         List<TaskFour.Rectangle> rectangleList = new LinkedList<>();
  9.         Scanner scanner = new Scanner(System.in);
  10.         int matrixSize = 1001;
  11.         boolean[][] matrixGrid = new boolean[matrixSize][matrixSize];
  12.         int rectanglesNumber = Integer.parseInt(scanner.nextLine());
  13.         for (int i = 0; i < rectanglesNumber; i++) {
  14.             int x1 = scanner.nextInt();
  15.             int y1 = scanner.nextInt();
  16.             int x2 = scanner.nextInt();
  17.             int y2 = scanner.nextInt();
  18.             rectangleList.add(instance.new Rectangle(x1, y1, x2, y2));
  19.         }
  20.         System.out.println(countRectangleCells(matrixGrid, rectangleList));
  21.        
  22.     }
  23.  
  24.     public static int countRectangleCells(boolean[][] matrixGrid, List<TaskFour.Rectangle> rectangleList) {
  25.         int cells = 0;
  26.         for (TaskFour.Rectangle rectangle : rectangleList) {
  27.             for (int x = rectangle.firstAngleXCoordinate; x <= rectangle.secondAngleXCoordinate; x++) {
  28.                 for (int y = rectangle.firstAngleYCoordinate; y <= rectangle.secondAngleYCoordinate; y++) {
  29.                     if (!matrixGrid[x][y]) {
  30.                         matrixGrid[x][y] = true;
  31.                         cells++;
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         return cells;
  37.     }
  38.  
  39.     class Rectangle {
  40.         int firstAngleXCoordinate;
  41.         int firstAngleYCoordinate;
  42.         int secondAngleXCoordinate;
  43.         int secondAngleYCoordinate;
  44.  
  45.         public Rectangle(int firstAngleXCoordinate, int firstAngleYCoordinate, int secondAngleXCoordinate, int secondAngleYCoordinate) {
  46.             this.firstAngleXCoordinate = firstAngleXCoordinate;
  47.             this.firstAngleYCoordinate = firstAngleYCoordinate;
  48.             this.secondAngleXCoordinate = secondAngleXCoordinate;
  49.             this.secondAngleYCoordinate = secondAngleYCoordinate;
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement