zoukankan      html  css  js  c++  java
  • POJ1151Atlantis(扫描线求面积并+线段树+离散化)

    Atlantis
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 11681   Accepted: 4537

    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"algorithm"

    #include"math.h"

    using namespace std;

     

    //#define eps 1e-6 

     

    struct Rect{

        double x1;

        double y1;

        double x2;

        double y2;

    };

     

    double x[300],y[300];//x和y坐标

    //bool vis[300][300];

    int findxi(double xx,int max)//利用线段树寻找xi和yi

    {

    int l=0,r=max-1,mid;

    while(l<=r)

    {

    mid=(l+r)/2;//printf("%d %d %lf\n",l,r,xx[mid]);

    if(x[mid]==xx) break;

    else if(x[mid]<xx)

    l=mid+1;

    else

    r=mid-1;

    }//printf("---%d\n",mid);

    return mid;

    }

     

    int findyi(double yy,int max)

    {

    int l=0,r=max-1,mid;

    while(l<=r)

    {

    mid=(l+r)/2;//printf("%d %d %lf\n",l,r,xx[mid]);

    if(y[mid]==yy) break;

    else if(y[mid]<yy)

    l=mid+1;

    else if(y[mid]>yy)

    r=mid-1;

    }//printf("---%d\n",mid);

    return mid;

    }

     

    int main(){

    int Case=0;

    int n=0;//多边形数目

    int xy[300][300];//保存离散化所得坐标:xy[i][j]=0则空白,xy[i][j]=1则填充

    Rect rect[300];//=new Rect[n];//多边形数组

    int num=0;//点数目

    while(scanf("%d",&n)&&n){

        //cout<<"n: "<<n<<endl;

        Case++;

        //初始化

        num=0;

        memset(rect,0,sizeof(rect));

        memset(x,0,sizeof(x));

        memset(y,0,sizeof(y));

        //读入多边形并将顶点x和y坐标分别保存在x[]和y[]数组(x[]和y[]数组后期需要排序和离散化处理)

        for(int i=0;i<n;i++){

        cin>>rect[i].x1>>rect[i].y1>>rect[i].x2>>rect[i].y2;

        //检测调整多边形左上角和右下角断点排序             

        x[num]=rect[i].x1;y[num]=rect[i].y1;

        num++;

        x[num]=rect[i].x2;y[num]=rect[i].y2;

        num++;

        }

        //cout<<"num: "<<num<<endl;//测试num是不是n的两倍

        //对x[]和y[]进行排序

        sort(x,x+num);

        sort(y,y+num);

        //对x[]和y[]做离散化处理,处理xy[]赋值

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

        int i1,i2;

        int j1,j2;

        for(int index=0;index<n;index++){//外层遍历多边形

            i1=findxi(rect[index].x1,num);

            i2=findxi(rect[index].x2,num);

            j1=findyi(rect[index].y1,num);

            j2=findyi(rect[index].y2,num);

            /*

        //内层分别遍历多边形4个点,与x[]和y[]进行index匹配并对xy[][]的值进行标注

            for(i1=0;i1<num;i1++)

                if(fabs(rect[index].x1-x[i1])<=eps)

                    break;

            for(i2=0;i2<num;i2++)

                if(fabs(rect[index].x2-x[i2])<=eps)

                    break;

            for(j1=0;j1<num;j1++)

                if(fabs(rect[index].y1-y[j1])<=eps)

                    break;

            for(j2=0;j2<num;j2++)

                if(fabs(rect[index].y2-y[j2])<=eps)

                    break;

                    */

            for(int i=i1;i<i2;i++)//对xy[][]进行标注

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

                    xy[i][j]=1;

     

        }//end for

        /*

        for(int i=0;i<num;i++){

            for(int j=0;j<num;j++)

                cout<<xy[i][j]<<" ";

            cout<<endl;

        }

        */

        //处理sum

        double sum=0.0;//结果

        for(int i=0;i<num;i++)

            for(int j=0;j<num;j++)

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

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

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

    }

    }

  • 相关阅读:
    DataTable常用操作总结[转帖]
    Jquery实现淡入淡出效果
    在自定义Server Control中捆绑JS文件 [转帖]
    ASP.NET利用String.Join以分隔符號來串連集合資料 [转帖]
    fork()的一些测试
    大端小端表示法 && GAS对过程的实现
    GAS中流程控制的实现,for, while, if, switch
    一个看起来奇怪的C++程序 && c++操作符重载
    修改函数的返回地址
    stl algorithm sort ,unique
  • 原文地址:https://www.cnblogs.com/lzhitian/p/2593747.html
Copyright © 2011-2022 走看看