topic
Problem Description
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.
Give all to draw a rectangle, I ask how many total units of area to be painted with colors.
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.
1 1 4 4
2 3 6 5
Thinking
The key question is how this squares represent my idea is relatively simple, is to create a two-dimensional array, then the "grid coordinates of the lower left corner" as "the entire grid of coordinates," has a painted box set . This left for a simple loop, if the operation has not painted; not painted on the set.
Would also have thought it would not be too easy, it would have missed what was happening. It also requires complex thought for a moment than to coordinate two boxes ah, less repetitive area. Such color Dang operating more on the complex.
Then try a few sample no problem, thinking that will not leak Diansha case, but found no problem trying to submit the matter. Sometimes still can not think too complicated, there is the idea to try to achieve, if lucky hit of it.
answer
#include<iostream> using namespace std; #define MAX 100 int nodes[MAX][MAX]; int main(){ int n; scanf("%d",&n); int count=0; while(n--){ int a,b,c,d; scanf("%d %d %d %d",&a,&b,&c,&d); for(int i=a;i<c;i++){ for(int j=b;j<d;j++){ if(nodes[i][j]==0){ nodes[i][j]=1; count++; } } } } printf("%d",count); return 0; }