[CCF201409-2] Drawing (super simple analysis)

The problem is described
  on a piece of paper that defines a rectangular coordinate system. Drawing a rectangle from (x1, y1) to (x2, y2) means to paint the area between the abscissa range from x1 to x2 and the ordinate range from y1 to y2. On the color.
  The following figure shows an example of drawing two rectangles. The first rectangle is (1,1) to (4, 4), represented in green and purple. The second rectangle is (2, 3) to (6, 5), represented in blue and purple. In the figure, a total of 15 units of area are painted with color, of which the purple part is painted twice, but only once when calculating the area. In the actual coloring process, all the rectangles are painted in a uniform color, and the different colors shown in the figure are only for convenience of illustration.

Insert picture description here

Given all the rectangles to be drawn, how many units of the area are colored.
Input format

The first line of input contains an integer n, which represents the number of rectangles to be drawn.
  
The next n rows, each with 4 non-negative integers, represent the abscissa and ordinate of the lower left corner, and the abscissa and ordinate of the upper right corner of the rectangle to be drawn.

Output format

Output an integer, indicating how many units of the area are colored.

Sample input

2
1 1 4 4
2 3 6 5

Sample output

15

Evaluation use case scale and conventions

1<=n<=100, 0<=abscissa, ordinate<=100.

Every time a rectangle is input, a traversal judgment is performed on the points in the rectangle (because they are all integer points are easy to interpret, in fact, the decimal point is also the same), if the current point has not been visited, the counter ++, and set the point has been marked . Note that the selected point starts from the vertex of the lower left corner. At this time, a small square is traversed. When looping, it should be smaller than the horizontal and vertical coordinates of the upper right corner. This can be proved by drawing by looping yourself
The structure used at the beginning was found to be unnecessary, and both submissions were correct. . . .
#include <iostream>
using namespace std;

int a[101][101];

int main(){
    
    
	int n;
	cin >> n;
	int cnt = 0;
	int x1,y1,x2,y2;
	for(int i = 0;i<n;i++){
    
    
		cin >> x1 >> y1 >> x2 >> y2;
		for(int j = x1;j<x2;j++){
    
    
			for(int k = y1;k<y2;k++){
    
    
				if(a[j][k]==0){
    
    
					cnt++;
					a[j][k] = 1;
				}
			}
		}
	}
	cout << cnt;
	return 0;
}
#include <iostream>
using namespace std;

typedef struct{
    
    
	int id;
	int x1,y1;//左下角 
	int x2,y2;//右上角 
}Paint; 

Paint pa[101];
int a[101][101];

int main(){
    
    
	int n;
	cin >> n;
	int cnt = 0;
	for(int i = 0;i<n;i++){
    
    
		cin >> pa[i].x1 >> pa[i].y1 >> pa[i].x2 >> pa[i].y2;
		for(int j = pa[i].x1;j<pa[i].x2;j++){
    
    
			for(int k = pa[i].y1;k<pa[i].y2;k++){
    
    
				if(a[j][k]==0){
    
    
					cnt++;
					a[j][k] = 1;
				}
			}
		}
	}
	cout << cnt;
	return 0;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/109282773