POJ 1899 Farmer Bill ' s problem

Source: Internet
Author: User
Tags bool integer numbers touch

Farmer Bill ' s problem

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2159 Accepted: 514

Description It is rumored, the planet Earth is often visited by unidentified Flying Objects (UFOs). Sometimes UFOs land and leave burned out regions. Observations show that these regions has the form of circles.

Recently farmer Bill has found such circles in his nice rectangular wheat field. Bill likes all mysterious things very much, so he had decided to keep these circles on the field. However, although being an ufolog, first of all Bill are the farmer, so he needs to harvest his wheat. Therefore he has decided to keep some regions containing circles intact, and harvest the rest of the field.

All regions this Bill keeps unharvested must be rectangles the neither touch nor overlap each other. The sides of the rectangles must is parallel to the sides of the field. All circles to UFOs must be inside these regions. The total area of the regions must is minimal possible, so-Bill could harvest the maximal possible part of its field.

Now, Bill wants to know, the total area of the field, that he'll be, able to harvest. Help him!

Input the first line of the input contains double integer numbers x and y-The dimensions of Bill ' s field (1 <= x, y < ; = 1000). Let Bill's field be positioned on the plane in such a-the-its corners is located in points with coordinates (0, 0), (x, 0), (x, y) and (0, y). The second line of the case contains N – the number of circles left by UFOs on Bill ' s field (0 <= N <= 100). Next N Lines describe Circles:each line contains three positive integer numbers XI, Yi and Ri--coordinates of the Cente R and radius of the circle. Circles may touch, overlap or contain. All circles is completely located within the field bounds.

Output output a single integer number-the area of the part of the field that Bill would be able to harvest.

Sample Input

8
2
3 3 1
1 1 1

Sample Output

64

Source Northeastern Europe 2003

 /* Pure calculation set, I see someone on the net with and check set, I feel cumbersome, direct scan on it can be 6155532 bobten2008 1899 Accepted 168K 32MS C + + 2003B 2009-11-21 17:25:11 */#i Nclude <iostream> #define MAXV (A, B) ((a) >= (b)? (a): (b) #define MINV (A, B) ((a) <= (b)? (a): (b) #define MAX_N, using namespace std; int x, y, num; struct REC {bool unvalid; int x1, y1, x2, y2;} Recs[max_n + 1]; Determine if the two lines intersect, the first part is the horizontal line, the latter part is the vertical bar of the data bool Segcross (int hx1, int hx2, int hy, int vx, int vy1, int vy2) {return VX >= hx1 &amp ;& VX <= hx2 && vy1 <= hy && hy <= vy2; }//Determine if two rectangles have a containing relationship bool in (const REC &AMP;R1, const rec &AMP;R2) {return (r2.x1 >= r1.x1 && r2.x2 <= r1.x2 && r2.y1 >= r1.y1 && r2.y2 <= r1.y2) | | (r1.x1 >= r2.x1 && r1.x2 <= r2.x2 && r1.y1 >= r2.y1 && r1.y2 <= r2.y2)); }//Determine if two rectangles intersect or contain bool Cross (const REC &AMP;R1, rec &r2) {if (Segcross (r1.x1, r1.x2, R1.y1, r2.x1, R2.y1, r2.y2) | | Segcross (r1.x1, r1.x2, R1.y1, r2.x2, R2.y1, r2.y2) | | Segcross (r1.x1, r1.x2, R1.y2, r2.x1, R2.y1, r2.y2) | | Segcross (r1.x1, r1.x2, R1.y2, r2.x2, R2.y1, r2.y2) | | Segcross (r2.x1, r2.x2, R2.y1, r1.x1, R1.y1, r1.y2) | | Segcross (r2.x1, r2.x2, R2.y1, r1.x2, R1.y1, r1.y2) | | Segcross (r2.x1, r2.x2, R2.y2, r1.x1, R1.y1, r1.y2) | | Segcross (r2.x1, r2.x2, R2.y2, r1.x2, R1.y1, r1.y2) | | In (R1, R2)) {r2.x1 = MINV (r1.x1, r2.x1); r2.y1 = Minv (r1.y1, r2.y1); r2.x2 = MAXV (r1.x2, r2.x2); r2.y2 = MAXV (R1.y2, R2.Y 2); return true; } return false; } int main () {int I, J, x0, y0, R, scanf ("%d%d%d", &x, &y, &num), for (i = 1; I <= num; i++) {scanf ("%d%d% D ", &x0, &y0, &r); recs[i].x1 = X0-r; Recs[i].y1 = Y0-r; recs[i].x2 = x0 + R; Recs[i].y2 = y0 + R; } bool change = TRUE; Note that this outer loop must be added, otherwise you may miss a lot of rectangles, I wa in this while (change) {change = false; for (i = 1; i < num; i++) {if (RECS[I].UNVALID) cont Inue; for (j = 1; j <= Num; j + +) {if (i = = J | | recs[j].unvalid) continue; if (Cross (Recs[i], recs[j]) {change = true; Recs[i].unvalid = true; Break }}}} int total = x * y; for (i = 1; I <= num, i++) if (!recs[i].unvalid) Total-= (recs[i].x2-recs[i].x1) * (RECS[I].Y2-RECS[I].Y1); printf ("%d/n", total); return 0; } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.