Solution
2023-07-19 09:37:37
发布于:广东
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> x(5); // we'll be using 1-indexing
vector<int> y(5);
for (int i = 1; i <= 4; i++) { cin >> x[i] >> y[i]; }
// Case 1
if (x[4] >= x[2] && x[3] <= x[1] && y[4] >= y[2] && y[3] <= y[1]) {
cout << 0;
}
// Case 2
else if (x[3] <= x[1] && y[3] <= y[1] && y[4] > y[1] && x[4] >= x[2]) {
cout << (x[2] - x[1]) * (y[2] - y[4]);
}
// Case 3
else if (y[3] < y[2] && x[3] <= x[1] && y[4] >= y[2] && x[4] >= x[2]) {
cout << (x[2] - x[1]) * (y[3] - y[1]);
}
// Case 4
else if (x[4] > x[1] && x[3] <= x[1] && y[4] >= y[2] && y[3] <= y[1]) {
cout << (x[2] - x[4]) * (y[2] - y[1]);
}
// Case 5
else if (x[3] < x[2] && x[4] >= x[2] && y[4] >= y[2] && y[3] <= x[1]) {
cout << (x[3] - x[1]) * (y[2] - y[1]);
}
// Case 6 and the corner case
else {
cout << (x[2] - x[1]) * (y[2] - y[1]);
}
}
As the problem statement says, the remaining cow feed billboard is situated in front of the lawnmower billboard, potentially obscuring it; therefore, we can split it into six cases to consider.
Images will be used to visualize the cases, so note these things: The red color shows the area covered by the second rectangle (whose borders are black), while the green one represents the area of the first rectangle (whose edges are blue). If two rectangles intersect, the red color will appear as the cow feed billboard block the lawnmower billboard.
We have to calculate the area of the first rectangle not obscured by the second one. There are 6 cases to consider, illustrated by the images below:
Case 1
In this case, both rectangles have the same coordinates, or the first rectangle lies in the area of the second one, so the answer is 0.
Case 2
Case 3
Case 4
Case 5
Case 6
The sixth case also includes a corner case where two rectangles intersect with their corners. In this case, if the intersection of two rectangles is at the corners (the top/down-left/right corners of the rectangles), we have to calculate the entire area of the first rectangle (the blue-edged rectangle). The image below illustrates the case:
这里空空如也
有帮助,赞一个