CCF-CSP 201409-2 drawing

The problem is described
  on a paper that defines a rectangular coordinate system. Draw a rectangle from (x1, y1) to (x2, y2) to paint the area between the horizontal coordinate range from x1 to x2 and the vertical coordinate range from y1 to y2 Color.
  The following figure shows an example of drawing two rectangles. The first rectangle is (1,1) to (4, 4), represented by green and purple. The second rectangle is (2, 3) to (6, 5), which is represented by blue and purple. In the figure, a total of 15 units of area are painted, of which the purple part is painted twice, but only once when calculating the area. In the actual painting process, all the rectangles are painted in a uniform color, and the different colors shown in the figure are just for convenience.
Illustration of this question
  Given all the rectangles to be drawn, how many units of the total area are painted.
Input format
  The first line of input includes an integer n, which represents the number of rectangles to be drawn.
  In the next n lines, each line has 4 non-negative integers, which respectively represent the horizontal and vertical coordinates of the lower left corner of the rectangle to be drawn, and the horizontal and vertical coordinates of the upper right corner.
Output format
  outputs an integer indicating how many units of area are painted.
Sample input
2
1 1 4 4
2 3 6 5
Sample output
15
Evaluation use case scale and convention
  1 <= n <= 100, 0 <= abscissa, ordinate <= 100.

Summary of experience:
Since the horizontal and vertical coordinates have a range (not less than 0 or greater than 100), you can create a two-dimensional array, assign the color-coordinated coordinates to 1 (that is, mark), and then traverse the array and add That's it.
Note: Array subscript mark.

C ++ code:

#include<bits/stdc++.h>
#define N 101
using namespace std;
int table[N][N];
int main() {
	int n,sum = 0;
	scanf("%d",&n);
	while(n--){
		int x1,y1,x2,y2;
		scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
		for(int i=x1;i<x2;i++){
			for(int j=y1;j<y2;j++){
				table[i][j] = 1;
			}
		}
	}
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			sum += table[i][j];
		}
	}
	printf("%d",sum);
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100538013