CSP201409-2: drawing, csp201409-2 drawing
Introduction:CSP (http://www.cspro.org/lead/application/ccf/login.jsp) is a "Computer vocational qualification certification" examination initiated by CCF, perform capability certification for professionals in computer software development, software testing, information management, and other fields. The subjects of certification are those who are engaged in or will be engaged in IT professional technical and technical management personnel, as well as review subjects for university recruitment and postgraduate students.
On a paper that defines the Cartesian coordinate system, draw a rectangle from (x1, y1) to (x2, y2) to convert the abscissa from x1 to x2, colors the area from y1 to y2.
An example of drawing two rectangles is given. The first rectangle is (1, 1) to (4, 4), expressed 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 colored, and the purple part is painted twice, but the area is calculated only once. In the actual coloring process, all rectangles are painted in a uniform color. Different colors are shown in the figure for convenience.
Show all the rectangles to be painted. How many units of area are painted.
The first line of the input contains an integer n, indicating the number of rectangles to be drawn.
In the next n rows, each line has four non-negative integers, indicating the abscissa and ordinate in the lower left corner of the rectangle to be drawn, and the abscissa and ordinate in the upper right corner.
The output is an integer indicating the number of units in which the area is colored.
# Include <stdio. h> # Include <stdlib. h> # Include <memory. h> # Define N 101 // Coordinates Struct pos { Int x1; Int y1; Int x2; Int y2; }; Int main (void) { Int n; // Number of rectangles Int area = 0; Int canvas [N] [N] = {0}; // all vertices in the coordinate system. 0 indicates that they are not colored, and 1 indicates that they have been colored. Scanf ("% d", & n ); Pos * pPst = (pos *) malloc (sizeof (pos) * n ); Memset (pPst, 0, sizeof (pos) * n ); For (int I = 0; I <n; I ++) { Scanf ("% d", & (pPst [I]. x1 )); Scanf ("% d", & (pPst [I]. y1 )); Scanf ("% d", & (pPst [I]. x2 )); Scanf ("% d", & (pPst [I]. y2 )); } For (int I = 0; I <n; I ++) { For (int m = pPst [I]. x1; m <pPst [I]. x2; m ++) { For (int n = pPst [I]. y1; n <pPst [I]. y2; n ++) { If (canvas [m] [n] = 0) { Area + = 1; Canvas [m] [n] = 1; } } } } Printf ("% d \ n", area ); Free (pPst ); Return 0; } |