【题目链接】 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;
}

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

  1. poj 3293 Rectilinear polygon

    Rectilinear polygon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2125   Accepted: 24 ...

  2. poj 2007 Scrambled Polygon(极角排序)

    http://poj.org/problem?id=2007 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6701   A ...

  3. POJ 1179 IOI1998 Polygon

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5472   Accepted: 2334 Description Polyg ...

  4. POJ 2007 Scrambled Polygon 凸包

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7214   Accepted: 3445 ...

  5. POJ 2007 Scrambled Polygon [凸包 极角排序]

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8636   Accepted: 4105 ...

  6. ●POJ 2007 Scrambled Polygon

    题链: http://poj.org/problem?id=2007 题解: 计算几何,极角排序 按样例来说,应该就是要把凸包上的i点按 第三像限-第四像限-第一像限-第二像限 的顺序输出. 按 叉积 ...

  7. POJ 2318 - TOYS - [计算几何基础题]

    题目链接:http://poj.org/problem?id=2318 Time Limit: 2000MS Memory Limit: 65536K Description Calculate th ...

  8. POJ 2007 Scrambled Polygon 极角序 水

    LINK 题意:给出一个简单多边形,按极角序输出其坐标. 思路:水题.对任意两点求叉积正负判断相对位置,为0则按长度排序 /** @Date : 2017-07-13 16:46:17 * @File ...

  9. 简单几何(极角排序) POJ 2007 Scrambled Polygon

    题目传送门 题意:裸的对原点的极角排序,凸包貌似不行. /************************************************ * Author :Running_Time ...

随机推荐

  1. 【BZOJ 2006】[NOI2010]超级钢琴 ST

    我们先把所有最左端对应的最优右端入堆,eg: z  在[l,r](由题目给出的L,R决定)之间的最优解 y,然后出堆以后,再入堆z,y-1,z,y+1,那么我们只需要用st找最大前缀和就好了(ST是一 ...

  2. Fabric证书解析

    一.证书目录解析   通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下:   ca: 存放组织的根证书和对应的私 ...

  3. ionic3自定义图标

    http://blog.csdn.net/qq993284758/article/details/78107412

  4. 关于javascript中的this 一段小实例深有体会啊

    先声明鄙人正在努力的把脚抬进门来,说的都是比较粗浅的知识,但都是我实践中得出的体会,很深刻.  正在自学中挣扎的DOG. 先看段代码: function highlightRows() { if(!d ...

  5. salt-api的使用

    curl -k https://192.168.74.129:8006/ -H "Accept: application/x-yaml" -H "X-Auth-Token ...

  6. 【BZOJ2253】纸箱堆叠 [CDQ分治]

    纸箱堆叠 Time Limit: 30 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description P 工厂是一个生产纸箱的工厂. 纸 ...

  7. 地震(quake)

    地震 题目描述 一场地震毁了 Farmer John 的整个农场.他是个有恒心的人,决定重建农场.在重建了所有 n(1<=n<=400)块田野后,他意识到还得修路将它们连起来.完工后,任两 ...

  8. concurrent

    from concurrent.futures import ThreadPoolExecutor

  9. 怎么重启shell ubuntu

    sunosfind . -type f  | xargs grep count 怎么重启shell ubuntu方法一:退出,重新登录方法二:source /etc/profile

  10. 使用libssh2库实现支持密码参数的ssh2客户端

    使用libssh2库实现支持密码参数的ssh2客户端 http://blog.chinaunix.net/uid-24382173-id-229823.html libssh2的简单应用 http:/ ...