Poj 1151-Atlantis 矩形切割

Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20276   Accepted: 7657

Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input consists of several test cases. Each test case starts with a line containing a single integer n (1 <= n <= 100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area. 
The input file is terminated by a line containing a single 0. Don't process it.

Output

For each test case, your program should output one section. The first line of each section must be "Test case #k", where k is the number of the test case (starting with 1). The second one must be "Total explored area: a", where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point. 
Output a blank line after each test case.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1
Total explored area: 180.00 

Source

 

题解:

矩形切割

这道题和 poj2528 类似,只不过变成了矩形。

具体可以看上一篇博客,关于线段切割的概括。

然后就多加两个判断就好了。

程序很好写,时间也不错(比线段树高到不知道哪里去了,我和它谈笑风生😄)

注意:相邻两个数据间要输出换行。PE了一发。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define MAXN 110
 4 int n;
 5 double XX1[MAXN],YY1[MAXN],XX2[MAXN],YY2[MAXN],ans[MAXN];
 6 void Cover(double X1,double Y1,double X2,double Y2,int k,int k1)
 7 {
 8     while(k<=n&&(X1>=XX2[k]||X2<=XX1[k]||Y1>=YY2[k]||Y2<=YY1[k]))k++;
 9     if(k>=n+1){ans[k1]+=(X2-X1)*(Y2-Y1);return;}
10     if(X1<XX1[k])
11     {
12         Cover(X1,Y1,XX1[k],Y2,k+1,k1);
13         X1=XX1[k];
14     }
15     if(X2>XX2[k])
16     {
17         Cover(XX2[k],Y1,X2,Y2,k+1,k1);
18         X2=XX2[k];
19     }
20     if(Y1<YY1[k])
21     {
22         Cover(X1,Y1,X2,YY1[k],k+1,k1);
23         Y1=YY1[k];
24     }
25     if(Y2>YY2[k])
26     {
27         Cover(X1,YY2[k],X2,Y2,k+1,k1);
28         Y2=YY2[k];
29     }
30 }
31 int main()
32 {
33     int i,case1=0;
34     double sum;
35     while(1)
36     {
37         scanf("%d",&n);if(n==0)break;
38         for(i=1;i<=n;i++){scanf("%lf %lf %lf %lf",&XX1[i],&YY1[i],&XX2[i],&YY2[i]);}
39         memset(ans,0,sizeof(ans));
40         for(i=n;i>=1;i--)Cover(XX1[i],YY1[i],XX2[i],YY2[i],i+1,i);
41         sum=0.0;
42         for(i=1;i<=n;i++)sum+=ans[i];
43         printf("Test case #%d\n",++case1);
44         printf("Total explored area: %.2lf\n\n",sum);
45     }
46     fclose(stdin);
47     fclose(stdout);
48     return 0;
49 }

 

posted @ 2016-04-08 11:36  微弱的世界  阅读(274)  评论(0编辑  收藏  举报