POJ 3293 Rectilinear polygon(几何基础)

【题目链接】 http://poj.org/problem?id=3293

【题目大意】

  给出一些点,每个点只能向外引出一条平行X轴,和Y轴的边,
  问能否构成一个闭多边形,如果能,返回多边形的总边长,否则返回-1

【题解】

  我们发现对于每一行或者每一列都必须有偶数个点,且两两之间相邻才能满足条件
  所以我们将其连线之后判断是否可以构成一个封闭图形,同时还需要判断这些线是否会相交,
  如果相交即不成立

【代码】

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=100010;
struct Point{int x,y,id;}p[N];
struct Line{
    int d,x,y;
    Line(){}
    Line(int _d,int _x,int _y):d(_d),x(_x),y(_y){}
}l[N];
int cmp_x(Point a,Point b){
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
int cmp_y(Point a,Point b){
    if(a.y==b.y)return a.x<b.x;
    return a.y<b.y;
}
int con[N][2],n,ln,T;
int Check(Point a,Point b){
    int y=a.y,x1=a.x,x2=b.x;
    for(int i=0;i<ln;i++){
        if(x1<l[i].d&&x2>l[i].d&&l[i].x<y&&l[i].y>y)return 1;
    }return 0;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&p[i].x,&p[i].y);
            p[i].id=i;
        }int s=0,cnt=1,flag=0;
        ln=0;
        sort(p,p+n,cmp_x);
        for(int i=1;i<n&&!flag;i++){
            if(p[i].x!=p[i-1].x){
                if(cnt&1)flag=1;
                cnt=1;
            }else{
                cnt++;
                if((cnt&1)==0){
                    s+=p[i].y-p[i-1].y;
                    con[p[i].id][0]=p[i-1].id;
                    con[p[i-1].id][0]=p[i].id;
                    l[ln++]=Line(p[i].x,p[i-1].y,p[i].y);
                }
            }
        }sort(p,p+n,cmp_y);
        cnt=1;
        for(int i=1;i<n&&!flag;i++){
            if(p[i].y!=p[i-1].y){
                if(cnt&1)flag=1;
                cnt=1;
            }
            else{
                cnt++;
                if((cnt&1)==0){
                    s+=p[i].x-p[i-1].x;
                    con[p[i].id][1]=p[i-1].id;
                    con[p[i-1].id][1]=p[i].id;
                    if(Check(p[i-1],p[i]))flag=1;
                }
            }
        }int t=1,x=0,c=0;
        for(;;){
            x=con[x][t];
            t^=1; c++;
            if(x==0||flag)break;
        }if(c!=n)flag=1;
        if(flag)puts("-1");
        else printf("%d\n",s);
    }return 0;
}
时间: 2024-08-07 04:20:31

POJ 3293 Rectilinear polygon(几何基础)的相关文章

poj 3293 Rectilinear polygon

Rectilinear polygon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2125   Accepted: 249 Description Given is n points with integer coordinates in the plane. Is it is possible to construct a simple, that is non-intersecting, rectilinear

poj 2007 Scrambled Polygon 极角排序

1 /** 2 极角排序输出,,, 3 主要atan2(y,x) 容易失精度,,用 4 bool cmp(point a,point b){ 5 if(cross(a-tmp,b-tmp)>0) 6 return 1; 7 if(cross(a-tmp,b-tmp)==0) 8 return length(a-tmp)<length(b-tmp); 9 return 0; 10 } 11 **/ 12 #include <iostream> 13 #include <algo

poj 2007 Scrambled Polygon(极角排序)

http://poj.org/problem?id=2007 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6701   Accepted: 3185 Description A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments ar

POJ 2007 Scrambled Polygon(计算几何 叉积排序啊)

题目链接:http://poj.org/problem?id=2007 Description A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the vertices of the polygon. When one starts at any vertex of a close

POJ 2007 Scrambled Polygon(凸包)

Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7568   Accepted: 3604 Description A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the

POJ 2007 Scrambled Polygon 极角序 水

LINK 题意:给出一个简单多边形,按极角序输出其坐标. 思路:水题.对任意两点求叉积正负判断相对位置,为0则按长度排序 /** @Date : 2017-07-13 16:46:17 * @FileName: POJ 2007 凸包极角序.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <std

POJ 1127 Jack Straws 几何基础

Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are

POJ 2007 Scrambled Polygon

虽然A了但是完全不懂这题在干什么. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 105 using namespace std; struct point { int x,y; point (int x,int y):x(x),y(y) {} point () {} friend point operator - (point x

poj 1039 Pipe(几何基础)

Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9932   Accepted: 3045 Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the design phase of the new pipe shape th