• POJ1389Area of Simple Polygons


    Area of Simple Polygons
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2404   Accepted: 1207

    Description

    There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates. 

    Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point. 

    Example: Consider the following three rectangles: 

    rectangle 1: < (0, 0) (4, 4) >, 

    rectangle 2: < (1, 1) (5, 2) >, 

    rectangle 3: < (1, 1) (2, 5) >. 

    The total area of all simple polygons constructed by these rectangles is 18. 

    Input

    The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.

    Output

    For each test case, output the total area of all simple polygons in a line. 

    Sample Input

    0 0 4 4
    1 1 5 2
    1 1 2 5
    -1 -1 -1 -1
    0 0 2 2
    1 1 3 3
    2 2 4 4
    -1 -1 -1 -1
    -1 -1 -1 -1  

    Sample Output

    18
    10 

    Source

     

    #include"iostream"
    #include"algorithm"
    #include"math.h"
    using namespace std;
    const int LEN= 1005;
    struct Rect{
    int x1;
    int y1;
    int x2;
    int y2;
    };

    int x[2*LEN],y[2*LEN];//x和y坐标
    int xy[2*LEN][2*LEN];//保存离散化所得坐标:xy[i][j]=0则空白,xy[i][j]=1则填充

    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)//max为y[]元素个数
    {
    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 n=0;//多边形数目
    Rect rect[LEN];//多边形数组
    int num=0;//点数目
    bool flag1=false,flag2=true;
    while(1){
    //初始化
    n=0;//多边形数目
    num=0;//x或者y的数目
    memset(rect,0,sizeof(rect));
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    while(1){
    //读入多边形并将顶点x和y坐标分别保存在x[]和y[]数组(x[]和y[]数组后期需要排序和离散化处理)
    cin>>rect[n].x1>>rect[n].y1>>rect[n].x2>>rect[n].y2;
    if(rect[n].x1==-1&&rect[n].x2==-1&&rect[n].y1==-1&&rect[n].y2==-1){
    if(flag2==true)
    flag1=true;
    flag2=true;
    break;
    }
    flag2=false;
    //检测调整多边形左上角和右下角断点排序
    x[num]=rect[n].x1;y[num]=rect[n].y1;
    num++;
    x[num]=rect[n].x2;y[num]=rect[n].y2;
    num++;
    n++;//多边形数自增
    }
    //判断是否跳出外循环
    if(flag1&&flag2)
    break;
    flag1=false;
    //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
    int sum=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]);
    cout<<sum<<endl;
    }
    //system("pause");
    }

  • 相关阅读:
    获取目录下所有文件名
    毕业论文endnote使用
    CoinChange
    sublime3个人配置
    2015-12-31
    2015-12-09
    #define DEBUG用法
    fiddler介绍
    app测试模块
    android SDK_安装配置_使用
  • 原文地址:https://www.cnblogs.com/lzhitian/p/2605833.html
Copyright © 2020-2023  润新知