把坐标离散化,这样横坐标最多有200个,竖坐标最多有200个。
然后标记一下那些块含盖。
然后计算面积。
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
using namespace std;
struct are
{double x1,y1,x2,y2;int ax,ay,bx,by;
} node[201];
double xx[501];
double yy[501];
int maps[501][501];
int numx,numy;
int n;
int cas;
void es()
{int i;sort(xx+1,xx+n*2+1);sort(yy+1,yy+n*2+1);numx=numy=2;for(i=2; i<=2*n; i++){if(xx[i]!=xx[i-1]){xx[numx]=xx[i];numx++;}if(yy[i]!=yy[i-1]){yy[numy]=yy[i];numy++;}}
}
void chu()
{int i,j;for(i=1; i<=n; i++){for(j=1; j<numx; j++){if(xx[j]==node[i].x1){node[i].ax=j;}if(xx[j]==node[i].x2){node[i].bx=j;}}for(j=1; j<numy; j++){if(yy[j]==node[i].y1){node[i].ay=j;}if(yy[j]==node[i].y2){node[i].by=j;}}}
}
void dos()
{int i,j,k;for(i=1;i<=n;i++){for(j=node[i].ax;j<node[i].bx;j++){for(k=node[i].ay;k<node[i].by;k++){maps[j][k]=1;}}}double ans=0.0;for(i=1;i<numx;i++){for(j=1;j<numy;j++){if(maps[i][j]==1){ans+=(xx[i+1]-xx[i])*(yy[j+1]-yy[j]);}}}printf("Test case #%d\nTotal explored area: ",cas);printf("%.2f\n",ans);
}
int main()
{int i;cas=0;while(~scanf("%d",&n)&&n){cas++;memset(maps,0,sizeof(maps));for(i=1; i<=n; i++){scanf("%lf%lf%lf%lf",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2);xx[(i-1)*2+1]=node[i].x1;xx[i*2]=node[i].x2;yy[(i-1)*2+1]=node[i].y1;yy[i*2]=node[i].y2;}es();chu();dos();cout<<endl;}return 0;
}