首页 > 其他 > 详细

POJ 1151 Atlantis

时间:2014-03-04 13:50:59      阅读:478      评论:0      收藏:0      [点我收藏+]
Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16244   Accepted: 6183

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


 原理是线段的切割
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define N 100000
using namespace std;
struct num
{
    double x1,y1,x2,y2;
}a[N],b[N];
int Top,pos;
bool uv;
bool cmp(const struct num &p1,const struct num &p2)
{
    return p1.x1<p2.x1;
}
int main()
{
    //freopen("data.txt","r",stdin);
    void ch();
    void add(double x1,double y1,double x2,double y2,int p);
    int n;
    int T=1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        {
            break;
        }
        for(int i=0;i<=n-1;i++)
        {
            scanf("%lf %lf %lf %lf",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
        }
        sort(a,a+n,cmp);
        Top = 0;
        for(int i=0;i<=n-1;i++)
        {
            pos = Top;
            double x3 = a[i].x1,y3 = a[i].y1;
            double x4 = a[i].x2,y4 = a[i].y2;
            bool in = false;
            for(int j=0;j<=Top-1;j++)
            {
               double x1 = b[j].x1,y1 = b[j].y1;
               double x2 = b[j].x2,y2 = b[j].y2;
               if(x3>x2||x1>x4||y3>y2||y1>y4)
               {
                   continue;
               }
               in = true;
               pos = j;
               uv = false;
               double k1 = max(x1,x3);
               double k2 = min(x4,x2);
               if(k1>x1)
               {
                   add(x1,y1,k1,y2,pos);
                   ch();
               }
               if(k2<x2)
               {
                   add(k2,y1,x2,y2,pos);
                   ch();
               }
               double k3 = max(y3,y1);
               double k4 = min(y4,y2);
               if(k3>y1)
               {
                   add(k1,y1,k2,y3,pos);
                   ch();
               }
               if(k4<y2)
               {
                   add(k1,y4,k2,y2,pos);
                   ch();
               }
            }
            add(x3,y3,x4,y4,pos);
            if(!in)
            {
                Top++;
                continue;
            }
            ch();
        }
        double sum = 0;
        for(int i=0;i<=Top-1;i++)
        {
            sum+=(b[i].x2-b[i].x1)*(b[i].y2-b[i].y1);
        }
        printf("Test case #%d\n",T++);
        printf("Total explored area: %.2lf\n",sum);
        printf("\n");
    }
    return 0;
}
void ch()
{
    if(uv)
    {
        Top++;
    }
    pos = Top;
    uv = true;
}
void add(double x1,double y1,double x2,double y2,int p)
{
    b[p].x1 = x1;
    b[p].y1 = y1;
    b[p].x2 = x2;
    b[p].y2 = y2;
}


POJ 1151 Atlantis,布布扣,bubuko.com

POJ 1151 Atlantis

原文:http://blog.csdn.net/yongxingao/article/details/20400867

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!