【题目链接】 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. HZOI String STL的正确用法

                                                                      String          3s 512 MB描述硬盘中里面有n ...

  2. Java的Properties使用及格式定义

    java.util.Properties extends Hashtable<Object,Object> 方便读取 键值对 格式的文本资源工具 常用方法一览 初始化对象 new Prop ...

  3. 做一个所见即所得的CSS效果

    style 也是标签(在非ie内核的浏览器中支持),我们将style设置成 contenteditable的时候,那么那的内容就可以编辑了.仔细的体验下,如果我们将背景修改成红色的.那么只要书写完,立 ...

  4. yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY

    yum命令Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY 博客分类: linux   三种解决方案 我采取第三种方案解决的 第一种: linu ...

  5. idea 的http client的使用

    1.打开idea的http client的工具步骤如下图所示: 然后在http client 的工具里写:请求头,请求参数,请求体即可.

  6. DSP投放进阶指南

  7. java类中获取WEB-INF路径

    WEB-INF路径 String path = WsTestBOImpl.class.getClass().getResource("/").getPath(); path = p ...

  8. python 读 excel 模块: xlrd

    主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python ...

  9. 一致性hash与CRUSH算法总结

    相同之处:都解决了数据缓存系统中数据如何存储与路由. 不同之处:区别在于虚拟节点和物理节点的映射办法不同 由于一般的哈希函数返回一个int(32bit)型的hashCode.因此,可以将该哈希函数能够 ...

  10. QML与C++混合编程详解(转)

    原文转自:http://blog.csdn.net/ieearth/article/details/42243553 原文转自:https://www.cnblogs.com/findumars/p/ ...