ccf140902- Paint

topic

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.

  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
Evaluation scale cases and agreed with
  1 <= n <= 100,0 <= abscissa, ordinate <= 100.

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

 

Guess you like

Origin www.cnblogs.com/lyeeer/p/11517146.html