The 29th CCF CSP certification question first question 202303-1 field measurement C++ to achieve full score answer

 

Problem Description

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

Recently, Dundun wants to reclaim a rectangular field with an area of ​​a×b at the foot of Nanshan Mountain. The coordinates of the lower left corner are (0,0) and the coordinates of the upper right corner are (a,b). Try to calculate the area of ​​the fields that already exist in the selected area of ​​Dundun.

input format

Read data from standard input.

Input a total of n+1 lines.

The first line of input contains three positive integers n, a, and b separated by spaces, denoting the number of field plots on Sisiever Island and the coordinates of the upper right corner of the selected area of ​​Dundon, respectively.

The next n lines each contain space-separated four 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 of ​​Dundun.

sample input

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

Data

sample output

44

Data

sample explanation

As shown, the area of ​​the field (green area) in the selected area is 44.

 

Subtasks

All test data satisfy n≤100, and the absolute value of all input coordinates does not exceed 104.

 

Problem-solving ideas:

The essence is to find the intersection area between matrices, and find out the law by drawing and observing. The area of ​​the intersection part (take two rectangles as an example), the right boundary of the intersection part matrix is ​​the smaller value of the right boundary of the original two large matrices, and the left boundary is the larger value of the left boundary of the original two large matrices. Therefore, the width of the intersection matrix is ​​the right boundary minus the left boundary x = min(a, x2) - max(0, x1); similarly, the relationship between the upper boundary and the lower boundary y = min(b, y2) - max(0, y1); Finally, judge whether there is an intersection between the two matrices by judging whether x and y are greater than zero, and if so, sum += x * y;

The C++ full score code is as follows:

#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;
}
 

Summarize

As the first question of the CCF CSP certification exam, the amount of code is generally not very long, and the for loop generally has only one layer. Pay attention to observing and summarizing the rules and refining the abstract model. In this question, some students use a large number of if statements to make conditional judgments, which is time-consuming and easy to miss, and the correct rate is low. As a result, the mentality of the first question collapses, which affects the performance of subsequent questions. Be careful not to get stuck and switch tactics in time.

Guess you like

Origin blog.csdn.net/sun_kingngfyncfh/article/details/129739117