Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11681 | Accepted: 4537 |
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
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
#include"iostream"
#include"algorithm"
#include"math.h"
using namespace std;
//#define eps 1e-6
struct Rect{
double x1;
double y1;
double x2;
double y2;
};
double x[300],y[300];//x和y坐标
//bool vis[300][300];
int findxi(double xx,int max)//利用线段树寻找xi和yi
{
int l=0,r=max-1,mid;
while(l<=r)
{
mid=(l+r)/2;//printf("%d %d %lf\n",l,r,xx[mid]);
if(x[mid]==xx) break;
else if(x[mid]<xx)
l=mid+1;
else
r=mid-1;
}//printf("---%d\n",mid);
return mid;
}
int findyi(double yy,int max)
{
int l=0,r=max-1,mid;
while(l<=r)
{
mid=(l+r)/2;//printf("%d %d %lf\n",l,r,xx[mid]);
if(y[mid]==yy) break;
else if(y[mid]<yy)
l=mid+1;
else if(y[mid]>yy)
r=mid-1;
}//printf("---%d\n",mid);
return mid;
}
int main(){
int Case=0;
int n=0;//多边形数目
int xy[300][300];//保存离散化所得坐标:xy[i][j]=0则空白,xy[i][j]=1则填充
Rect rect[300];//=new Rect[n];//多边形数组
int num=0;//点数目
while(scanf("%d",&n)&&n){
//cout<<"n: "<<n<<endl;
Case++;
//初始化
num=0;
memset(rect,0,sizeof(rect));
memset(x,0,sizeof(x));
memset(y,0,sizeof(y));
//读入多边形并将顶点x和y坐标分别保存在x[]和y[]数组(x[]和y[]数组后期需要排序和离散化处理)
for(int i=0;i<n;i++){
cin>>rect[i].x1>>rect[i].y1>>rect[i].x2>>rect[i].y2;
//检测调整多边形左上角和右下角断点排序
x[num]=rect[i].x1;y[num]=rect[i].y1;
num++;
x[num]=rect[i].x2;y[num]=rect[i].y2;
num++;
}
//cout<<"num: "<<num<<endl;//测试num是不是n的两倍
//对x[]和y[]进行排序
sort(x,x+num);
sort(y,y+num);
//对x[]和y[]做离散化处理,处理xy[]赋值
memset(xy,0,sizeof(xy));
int i1,i2;
int j1,j2;
for(int index=0;index<n;index++){//外层遍历多边形
i1=findxi(rect[index].x1,num);
i2=findxi(rect[index].x2,num);
j1=findyi(rect[index].y1,num);
j2=findyi(rect[index].y2,num);
/*
//内层分别遍历多边形4个点,与x[]和y[]进行index匹配并对xy[][]的值进行标注
for(i1=0;i1<num;i1++)
if(fabs(rect[index].x1-x[i1])<=eps)
break;
for(i2=0;i2<num;i2++)
if(fabs(rect[index].x2-x[i2])<=eps)
break;
for(j1=0;j1<num;j1++)
if(fabs(rect[index].y1-y[j1])<=eps)
break;
for(j2=0;j2<num;j2++)
if(fabs(rect[index].y2-y[j2])<=eps)
break;
*/
for(int i=i1;i<i2;i++)//对xy[][]进行标注
for(int j=j1;j<j2;j++)
xy[i][j]=1;
}//end for
/*
for(int i=0;i<num;i++){
for(int j=0;j<num;j++)
cout<<xy[i][j]<<" ";
cout<<endl;
}
*/
//处理sum
double sum=0.0;//结果
for(int i=0;i<num;i++)
for(int j=0;j<num;j++)
sum+=xy[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);
printf("Test case #%d\n",Case);
printf("Total explored area: %.2f\n\n",sum);
}
}