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 ...
随机推荐
- Codeforces Round #525 (Div. 2)B. Ehab and subtraction
B. Ehab and subtraction 题目链接:https://codeforc.es/contest/1088/problem/B 题意: 给出n个数,给出k次操作,然后每次操作把所有数减 ...
- oracle的group by问题
ORA-00979 不是 GROUP BY 表达式”这个错误,和我前面介绍的另外一个错误ORA-00937一样使很多初学oracle的人爱犯的. 我在介绍使用聚合函数中用group by来分组数据时特 ...
- spring aop与aspectj
AOP:面向切面编程 简介 AOP解决的问题:将核心业务代码与外围业务(日志记录.权限校验.异常处理.事务控制)代码分离出来,提高模块化,降低代码耦合度,使职责更单一. AOP应用场景: 日志记录.权 ...
- AtCoder Regular Contest 075 D Widespread
题目传送门 这道题其实二分一下答案就okay了的 不过LL什么的有时候忘了加 被卡了下 #include<cstdio> #include<cstring> #include& ...
- [POJ2954&POJ1265]皮克定理的应用两例
皮克定理: 在一个多边形中.用I表示多边形内部的点数,E来表示多边形边上的点数,S表示多边形的面积. 满足:S:=I+E/2-1; 解决这一类题可能运用到的: 求E,一条边(x1,y1,x2,y2)上 ...
- 1210笔记//关于导航实例-QQ空间//导航实例-storyboard实现//控制器的生命周期//控制器的生命周期方法
一.利用storyboard完成导航1.storyboard中用来跳转的每一根线 都是 一个 UIStoryboardSegue对象1> 自动跳转 (从 某个按钮 拖线到 下一个目的控制器) ...
- Web Application Vulnerabilities and Potential Problem Due to Bad Design
web应用设计中不安全的设计及潜在的风险: REF: https://msdn.microsoft.com/en-us/library/ff648647.aspx
- 如何加快Eclipse的启动速度?
http://it.taocms.org/12/6457.htm 浅析配置更快的Eclipse方法 What is the Metadata GC Threshold and how do I tun ...
- PHP性能追踪及分析工具xhprof的安装与使用
对于本地开发环境来说,进行性能分析xdebug是够用了,但如果是线上环境的话,xdebug消耗较大,配置也不够灵活,因此线上环境建议使用xhprof进行PHP性能追踪及分析. 我们今天就简单介绍一下x ...
- a标签里文本居中
text-align:center; height: 30px; line-height:30px;