Atlantis
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11113 | Accepted: 4359 |
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.
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.
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
多组数据要小心,我一开始没有换行,结果就输出格式错误。然后有输出格式错误。。发现是要输出两个空行。还有POJ的double输入要用lf,输出要用f。
对矩形切割的理解更深了。递归切割的条件,是首先已经满足了相交,然后如果下一个矩形的左边界能切割就用左边界切割,右边界能切割就用右边界切割,上下相同。
切割之后把已经算过面积的区域去掉。
#include <cstdio>
long n;
double x1[110];
double x2[110];
double y1[110];
double y2[110];
const double eps = 1e-8;
double ans = 0;
void Cut(double fx1,double fy1,double fx2,double fy2,long i)
{
while (i<n+1 && (fx1>x2[i]-eps || x1[i]>fx2-eps || fy1>y2[i]-eps || y1[i]>fy2-eps))i++;
if (i == n+1) {ans+=(fx2-fx1)*(fy2-fy1);return;}
if (x1[i]>fx1-eps){Cut(fx1,fy1,x1[i],fy2,i+1);fx1=x1[i];}
if (x2[i]<fx2+eps){Cut(x2[i],fy1,fx2,fy2,i+1);fx2=x2[i];}
if (y1[i]>fy1-eps){Cut(fx1,fy1,fx2,y1[i],i+1);}
if (y2[i]<fy2+eps){Cut(fx1,y2[i],fx2,fy2,i+1);}
}
int main()
{
freopen("atlantis.in","r",stdin);
freopen("atlantis.out","w",stdout);
long casec = 1;
while (1)
{
scanf("%ld",&n);
if (!n) break;
ans = 0;
for (long i=1;i<n+1;i++)
scanf("%lf%lf%lf%lf",x1+i,y1+i,x2+i,y2+i);
for (long i=n;i>0;i--)
Cut(x1[i],y1[i],x2[i],y2[i],i+1);
printf("Test case #%ld\nTotal explored area: %.2f\n\n",casec++,ans);
}
return 0;
}