ZOJ 1128

#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;

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值