[CSP Certification Exam] 202303-1: Field Measurement Problem Solving Ideas + Code

Problem Description

There are n fields scattered on Sisi Iver Island. Each field can be regarded as a rectangular area under the plane Cartesian coordinate system, uniquely determined by the lower left corner coordinates (x1, y1) and the upper right corner coordinates (x2, y2), and satisfies x1<x2, y1<y2. Among these n fields, the intersection area of ​​any two fields is 0, and only the boundaries may overlap.

Recently, Dundun wanted to cultivate a rectangular field with an area of ​​​​a×b at the foot of Nanshan, with the coordinates of the lower left corner as (0,0) and the upper right corner as (a,b). Try to calculate the existing field area in Dundun’s selected area.

Input format

Read data from standard input.

Enter a total of n+1 lines.

The first line of input contains three positive integers n, a and b separated by spaces, which respectively represent the number of field plots on West Iver Island and the coordinates of the upper right corner of the selected area in Dunton.

Next n lines, each line contains four space-separated integers x1, y1, x2 and y2, representing the location of a field.

Output format

Output to standard output.

Output an integer representing the field area within the selected area.

Sample input

4 10 10
0 0 5 5
5 -2 15 3
8 8 15 15
-2 10 3 15

Sample output

44

 

Idea:

First, just define the input vertex position according to the format. Then through observation, we can find that the rectangular intersection area we require is the larger value of the left boundary of the two large rectangles and the smaller value of the right boundary, subtracted to one side length, and the upper boundary The smaller value and the larger value of the lower boundary are subtracted to the length of the other side, and then the area of ​​a rectangle can be obtained by multiplying the lengths of both sides, and the calculated areas of the rectangles can be added in turn.

Code:

#include <iostream>
using namespace std;
int main()
{
    int n, a, b;
    int x1, y1, x2, y2;
    int x, y;
    int sum = 0;
    cin >> n >> a >> b;
    for(int i = 1; i <= n; i++){
        cin >> x1 >> y1 >> x2 >> y2;
        x = min(a, x2) - max(0, x1);
        y = min(b, y2) - max(0, y1);
        if(x >= 0 && y >= 0)
            sum += x * y;
    }
    cout << sum;
    return 0;
}

おすすめ

転載: blog.csdn.net/m0_63501513/article/details/133000345