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 ...
随机推荐
- Spring validation 后端校验【转】
本文来自 下一秒升华 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u013815546/article/details/77248003?utm_source=co ...
- ubuntu下使用sudo 出现unable to resolve host 解决方法
Linux 环境, 假设这台机器名字叫dev(机器的hostname), 每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host dev虽然sudo 还是可以正 ...
- 小K的农场
小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了 ...
- 牛客 国庆七天乐 day1 L
https://www.nowcoder.com/acm/contest/201/L 题意:给你两条平行的直线和n个圆,在直线上面行走和在圆上和在圆内行走不需要耗费体力,除了这些区域外平面上经过任意两 ...
- MFC 监控界面上所有文本框值的变化
//控件消息,菜单,按钮等 BOOL CXXDlg::OnCommand(WPARAM wParam, LPARAM lParam) { // TODO: 在此添加专用代码和/或调用基类 int wm ...
- node读取文件夹名
const fs = require('fs'); const join = require('path').join; /** * * @param startPath 起始目录文件夹路径 * @r ...
- django 连接 oracle 问题
安装 oracle 后,在 django 项目中连接出现问题记录. 问题1:pip install cx_Oacle 未出现任何问题,但运行过程出现: 原因:连接 oracle 的工具 cx_Orac ...
- JavaScript学习笔记——浅拷贝、深拷贝
参考自:http://www.cnblogs.com/yichengbo/archive/2014/07/10/3835882.html 一.数组的深浅拷贝 在使用JavaScript对数组进行操作的 ...
- RPC-整体概念
RPC概述 RPC(Remote Procedure Call),即远程过程调用,是一种通过网络从远程计算机程序上请求服务而不需要了解底层网络技术的协议,实现调用远程主机上的方法就像调用本地方法一样. ...
- 也说JS脚本加载控制
问题背景 前端采用的 iframe + html 做后台管理系统.现在js.jquery插件非常多,每次页面都是引用就类似这样: <script src="../Scripts/jqu ...