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;
}
POJ 3293 Rectilinear polygon(几何基础)的更多相关文章
- poj 3293 Rectilinear polygon
Rectilinear polygon Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2125 Accepted: 24 ...
- poj 2007 Scrambled Polygon(极角排序)
http://poj.org/problem?id=2007 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6701 A ...
- POJ 1179 IOI1998 Polygon
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5472 Accepted: 2334 Description Polyg ...
- POJ 2007 Scrambled Polygon 凸包
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7214 Accepted: 3445 ...
- POJ 2007 Scrambled Polygon [凸包 极角排序]
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8636 Accepted: 4105 ...
- ●POJ 2007 Scrambled Polygon
题链: http://poj.org/problem?id=2007 题解: 计算几何,极角排序 按样例来说,应该就是要把凸包上的i点按 第三像限-第四像限-第一像限-第二像限 的顺序输出. 按 叉积 ...
- POJ 2318 - TOYS - [计算几何基础题]
题目链接:http://poj.org/problem?id=2318 Time Limit: 2000MS Memory Limit: 65536K Description Calculate th ...
- POJ 2007 Scrambled Polygon 极角序 水
LINK 题意:给出一个简单多边形,按极角序输出其坐标. 思路:水题.对任意两点求叉积正负判断相对位置,为0则按长度排序 /** @Date : 2017-07-13 16:46:17 * @File ...
- 简单几何(极角排序) POJ 2007 Scrambled Polygon
题目传送门 题意:裸的对原点的极角排序,凸包貌似不行. /************************************************ * Author :Running_Time ...
随机推荐
- SCOI2008奖励关 [状压dp]
题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...
- UVA10480:Sabotage(最小割+输出)
Sabotage 题目链接:https://vjudge.net/problem/UVA-10480 Description: The regime of a small but wealthy di ...
- ng的ngModel用来处理表单操作
https://segmentfault.com/a/1190000009126012
- spring中@PropertySource注解的使用
概述: The @PropertySource annotation provides a convenient and declarative mechanism for adding aPrope ...
- centos7 mysql cluster集群搭建基于docker
1.准备 mn:集群管理服务器用于管理集群的其他节点.我们可以从管理节点创建和配置集群上的新节点.重新启动.删除或备份节点. db2/db3:这是节点间同步和数据复制的过程发生的层. db4/db5: ...
- 【Android开发日记】之入门篇(四)——Android四大组件之Activity
在Android中,无论是开发者还是用户,接触最多的就算是Activity.它是Android中最复杂.最核心的组件.Activity组件是负责与用户进行交互的组件,它的设计理念在很多方面都和Web页 ...
- .NET之特性和属性(转)
1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定 ...
- SpringMVC——如何获取请求参数
参考 http://www.cnblogs.com/bigdataZJ/p/springmvc2.html (文章讲了几个注解的使用,但不够深入.) 参考 http://www.cnblogs.com ...
- bzoj 1293 贪心
首先我们可以将这道题看成一个数轴,数轴其中的某些点存在一些颜色,我们要选取最短的一段,使这段存 在所有颜色,那么我们使用指针i,j表示在j-i位置中包含的颜色,那么初值是0,0,我们先i++,同时添加 ...
- Python小程序之sed命令替换
需求: 编写sed命令脚本 代码如下 # Author:Lee Sir import sys,os des_file = r'E:\StartPython\day3\test.txt' des_fil ...