poj 1151 Atlantis

poj 1151
Atlantis
Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

Description

There are several ancient Greek texts that contain descri_ptions 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 

[Submit]   [Go Back]   [Status]


/*

from:http://www.cppblog.com/RyanWang/archive/2009/02/22/74612.html

这道题是离散化的入门题。

问题描述:一些已知右下顶点和左上顶点坐标的矩形,这些矩形可能部分重叠,求它们所占的实际面积。

离散化

1、首先分离出所有的横坐标和纵坐标分别按升序存入数组X[ ]和Y[ ]中.

2、 设数组XY[ ][ ].对于每个矩形(x1,y1)(x2,y2)确定i1,i2,j1,j2,使得,X[i1]>x1,X[i2]<=x2,Y[i1]>y1,Y[i2]>=y2令XY[ i ][ j ] = 1 (i从i1到i2,j从j1到j2)

3、统计面积:area+=XY[i][j] *(X[i]-X[i-1])*(Y[i] – Y[i-1])


我一开始没弄明白。。。。。真是一个好的方法。。太巧妙了!

*/


#include<iostream>

using namespace std;

double x[201],y[201],s[101][4];

int xy[201][201];

int n,cas=0;

double sum;

int main()

{

    int i,j,k;

    while(cin>>n)

    {

        if(n==0)

            break;

        cas++;

        k=0;

        sum=0.0;

        memset(xy,0,sizeof(xy));

        for(i=1;i<=n;i++)

        {

            cin>>s[i][0]>>s[i][1]>>s[i][2]>>s[i][3];

            x[k]=s[i][0];

            y[k]=s[i][1];

            k++;

            x[k]=s[i][2];

            y[k]=s[i][3];

            k++;

        }

        sort(x,x+2*n);

        sort(y,y+2*n);

        for(k=1;k<=n;k++)

        {

            int i1,i2,j1,j2;

            for(i1=0;i1<2*n;i1++)

            {

                if(x[i1]==s[k][0])

                    break;

            }

            for(i2=0;i2<2*n;i2++)

            {

                if(x[i2]==s[k][2])

                    break;

            }

            for(j1=0;j1<2*n;j1++)

            {

                if(y[j1]==s[k][1])

                    break;

            }

            for(j2=0;j2<2*n;j2++)

            {

                if(y[j2]==s[k][3])

                    break;

            }

            for(i=i1;i<i2;i++)

            {

                for(j=j1;j<j2;j++)

                {

                    xy[i][j]=1;

                }

            }

        }

        for(i=0;i<2*n;i++)

        {

            for(j=0;j<2*n;j++)

            {

                sum+=xy[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);

            }

        }

        printf("Test case #%d\n",cas);

        printf("Total explored area: %.2f\n",sum);

        printf("\n");

    }

    system("pause");

    return 0;

}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值