CSP201409-2 Java program out

Problem Description

In a rectangular coordinate system defined on paper and draw a (x1, y1) to the rectangle (x2, y2) refers to the abscissa range from x1 to x2, from the region of the ordinate range y1 to y2 between the painted color .
  The following figure shows an example of two rectangles painted. The first is a rectangle (1, 1) to (4, 4), shown in green and purple. The second rectangle is (2, 3) to (6, 5), shown in blue and purple. Figure, a total of 15 units of area is coated with a color, wherein the portion is coated with violet twice, but in the calculation of the area is calculated only once. In an actual coloring process, all rectangles are painted a uniform color, a different color shown in the drawings merely for illustration.
 Here Insert Picture Description
Give all to draw a rectangle, I ask how many total units of area to be painted with colors.

Input Format

Comprising a first line of input integer n, the number of the rectangle to be drawn.
  Next n lines, each line 4 non-negative integer, respectively, and the ordinate abscissa represents the lower left corner of the rectangle to be drawn, and the abscissa and ordinate of the upper right corner.

Output Format

Output An integer representing the number of units of the area to be painted with colors.

Sample input

2
1 1 4 4
2 3 6 5

Sample Output

15

Sample Description

1 <= n <= 100,0 <= abscissa, ordinate <= 100.

CSP Exam: http://118.190.20.162/view.page?gpid=T15

Code

import java.util.Scanner;

public class Main {
    /*
    * 在画板board上填充(x1, y1) -> (x2, y2)的空格
    * @param board
    * 画板
    * @param x1
    * 左下角横坐标
    * @param y1
    * 左下角纵坐标
     */
    static void draw(Boolean[][] board, Integer x1, Integer y1, Integer x2, Integer y2){
        for(int i = x1; i < x2; i++){
            for(int j = y1; j < y2; j++){
                board[i][j] = true;
            }
        }
    }

    /*
    * 初始化画板board,全部初始化为false,代表空
     */
    static void initBoard(Boolean[][] board){
        for(int i = 0; i <= 100; i++){
            for(int j = 0; j <= 100; j++){
                board[i][j] = false;
            }
        }
    }

    public static void main(String[] args) {
        Boolean[][] board = new Boolean[101][101];
        initBoard(board);

        Scanner scanner = new Scanner(System.in);
        Integer n = scanner.nextInt();

        for(int i = 0; i < n; i++){
            Integer x1 = scanner.nextInt();
            Integer y1 = scanner.nextInt();
            Integer x2 = scanner.nextInt();
            Integer y2 = scanner.nextInt();
            draw(board, x1, y1, x2, y2);
        }

        Integer res = 0;
        for(int i = 0; i <= 100; i++){
            for(int j = 0; j <= 100; j++){
                if(board[i][j]){
                    res++;
                }
            }
        }
        System.out.println(res);
    }
}

Guess you like

Origin blog.csdn.net/weixin_43412579/article/details/93480324