#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
struct Rect{
double up, left, right, bottom;
Rect* next;
void insert(double u, double l, double r, double b);
void clear(){ if (next){ next->clear(); delete next; next = NULL; } };
};
void Rect::insert(double u, double l, double b, double r)
{
//inside this rect.
if (u >= up && b <= bottom && r <= right && l >=left) return;
//not intersect with this rect.
if (u >= bottom || b <= up || r <= left || l >=right)
{
if (next) next->insert(u, l, b, r);
else{
next = new Rect;
next->up = u; next->bottom = b;
next->left = l; next->right = r;
next->next=NULL;
}
return;
}
double mu, mb, ml, mr;
mu = u > up ? u : up; mb = b < bottom ? b : bottom;
ml = l > left ? l : left; mr = r < right ? r : right;
if (mu > u && ml > l) insert(u, l, mu, ml);
if (u < mu) insert(u, ml, mu, mr);
if (l < ml) insert(mu, l, b, ml);
if (mb < b) insert(mb, ml, b, r);
if (mr < r) insert(mu, mr, mb, r);
}
int main(int argc, char *argv[])
{
Rect rect={0, 0, 0, 0, NULL};
int n;
double u, l, b, r;
double area;
int t = 1;
while (cin>>n, n)
{
rect.clear();
for (int i = 0; i<n; i++)
{
cin>>u>>l>>b>>r;
rect.insert(u, l, b, r);
}
area = 0;
Rect *p = rect.next;
while (p)
{
area += (p->right-p->left)*(p->bottom - p->up);
p=p->next;
}
cout<<"Test case #"<<t++<<endl;
cout<<"Total explored area: "<<fixed<<setprecision(2)<<area<<endl;
cout<<endl;
}
}
ZOJ 1128
最新推荐文章于 2020-08-10 10:26:57 发布