pku 1151 Atlantis(离散化)

Atlantis
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14357 Accepted: 5516

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


题解:给定n个矩形的坐标,问得出的图形的面积(重叠部分不重复计算)
题解:听说是一个离散化+线段树的题目,但是我将x和y坐标都离散化就直接暴力过了.....本人是这样做的,将x和y坐标离散化,记录每一个分割出来的矩形是否被覆盖,由于只有最多二百个x坐标和二百个y坐标,然后直接二重循环扫描各小矩形是否被覆盖并计算小矩形面积复杂度也不高。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
double hasx[205],hasy[205];
int mark[205][205];
struct point{
    double x1,y1,x2,y2;
}p[105];
void swa(double *a,double *b)
{
    double temp;
    if(*a>*b) temp=*a,*a=*b,*b=temp;
}
int cmp(const void *a,const void *b)
{
    return (*(double *)a>*(double *)b)?1:-1;
}
int erfen(double x,double *has,int n)
{
    int l=0,r=n-1,mid;

    while(l<=r)
    {
        mid=(l+r)/2;
        if(has[mid]==x) return mid;
        if(has[mid]>x) r=mid-1;
        else l=mid+1;
    }
    return l;
}
void huoqianliuming(int x1,int y1,int x2,int y2)
{
    int i,j;
    for(i=x1;i<x2;i++)
    {
        for(j=y1;j<y2;j++)
            mark[i][j]=1;
    }
}
int main()
{
    int i,j,nx,ny,n,x[2],y[2],t=1;
    double temp,sum;

    while(scanf("%d",&n),n)
    {
        for(j=i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&p[i].x1,&p[i].y1,&p[i].x2,&p[i].y2);
            swa(&p[i].x1,&p[i].x2),swa(&p[i].y1,&p[i].y2);
            hasx[j]=p[i].x1,hasy[j++]=p[i].y1;
            hasx[j]=p[i].x2,hasy[j++]=p[i].y2;
        }
        qsort(hasx,2*n,sizeof(hasx[0]),cmp);
        qsort(hasy,2*n,sizeof(hasy[0]),cmp);
        //离散化x和y坐标,用hasx和hasy数组保存,删除重复的点
        for(i=j=1,temp=hasx[0];i<2*n;i++)
        {
            if(hasx[i]==temp) continue;
            hasx[j++]=temp=hasx[i];
        }
        nx=j;
        for(i=j=1,temp=hasy[0];i<2*n;i++)
        {
            if(hasy[i]==temp) continue;
            hasy[j++]=temp=hasy[i];
        }
        ny=j;
        memset(mark,0,sizeof(mark));
        //如果小矩形被覆盖,则使其左下角坐标对应的mark矩阵点赋值为1
        for(i=0;i<n;i++)
        {
            x[0]=erfen(p[i].x1,hasx,nx);
            y[0]=erfen(p[i].y1,hasy,ny);
            x[1]=erfen(p[i].x2,hasx,nx);
            y[1]=erfen(p[i].y2,hasy,ny);
            huoqianliuming(x[0],y[0],x[1],y[1]);
        }
        //计算总面积
        for(sum=i=0;i<=nx;i++)
        {
            for(j=0;j<=ny;j++)
            {
                if(mark[i][j])
                {
                    temp=(hasx[i+1]-hasx[i])*(hasy[j+1]-hasy[j]);
                    sum=sum+temp;
                }
            }
        }
        if(t!=1) printf("\n");
        printf("Test case #%d\n",t++);
        printf("Total explored area: %.2lf\n",sum);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值