Atlantis
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 7220
Accepted: 2793
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
Mid-Central European Regional Contest 2000
解析:
从左往右扫,将整个图形切割成小的矩形,将面积累加。。。
如图:按题中样例,将坐标离散化后对应序号为0,1,2,3
图中小矩形内的数字表示该矩形被累加的顺序
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
double x1[300],x2[300],y1[300],y2[300];
int n,xy[210][210],l;
double xx[210],yy[210];
void work()
{
freopen("poj1151.in","r",stdin);
freopen("poj1151.out","w",stdout);
int count=0;
while(cin>>n)
{
int i,j;
l=0;
if(n==0)break;
count++;
memset(xy,0,sizeof(xy));
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1[i],&y1[i],&x2[i],&y2[i]);
xx[l]=x1[i];yy[l]=y1[i];
l++;
xx[l]=x2[i];yy[l]=y2[i];
l++;
}
sort(xx,xx+2*n);
sort(yy,yy+2*n);
double sum=0.0;
int k;
for(k=1;k<=n;k++)
{
int i1,i2,j1,j2;
for(i1=0;i1<2*n;i1++)
if(xx[i1]==x1[k])break;
for(i2=0;i2<2*n;i2++)
if(xx[i2]==x2[k])break;
for(j1=0;j1<2*n;j1++)
if(yy[j1]==y1[k])break;
for(j2=0;j2<2*n;j2++)
if(yy[j2]==y2[k])break;
for(i=i1;i<i2;i++)
for(j=j1;j<j2;j++)
xy[i][j]=1;
}
for(i=0;i<2*n;i++)
for(j=0;j<2*n;j++)
sum+=xy[i][j]*(xx[i+1]-xx[i])*(yy[j+1]-yy[j]);
printf("Test case #%d\n",count);
printf("Total explored area: %.2f\n",sum);
printf("\n");
}
}
int main()
{
work();
return 0;
}