UVA 10256 The Great Divide (凸包,多边形的位置关系)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34148
【思路】
凸包
求出红蓝点的凸包,剩下的问题就是判断两个凸包是否相离。
需要确定两点:
1) 凸包上线段是否相交->相交
2) 凸包上的点是否包含在另一个凸包里->内含。
【代码】
#include<cmath>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std; const double eps = 1e-;
int dcmp(double x) {
if(fabs(x)<eps) return ; else return x<? -:;
} struct Pt {
double x,y;
Pt(double x=,double y=):x(x),y(y) {};
};
typedef Pt vec;
vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
bool operator == (Pt A,Pt B) {
return dcmp(A.x-B.x)== && dcmp(A.y-B.y)==;
}
bool operator < (const Pt& a,const Pt& b) {
return a.x<b.x || (a.x==b.x && a.y<b.y);
}
double Dot(vec A,vec B) { return A.x*B.x+A.y*B.y;}
double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; } bool SegIntersection(Pt a1,Pt a2,Pt b1,Pt b2) {
double c1 = cross(a2-a1,b1-a1), c2 = cross(a2-a1,b2-a1),
c3 = cross(b2-b1,a1-b1), c4=cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
}
bool OnSeg(Pt p,Pt a1,Pt a2) {
return dcmp(cross(p-a1,p-a2))== && dcmp(Dot(p-a1,p-a2))<;
} int n;
vector<Pt> ConvexHull(vector<Pt> p) {
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
int n=p.size();
int m=;
vector<Pt> ch(n+);
for(int i=;i<n;i++) {
while(m> && cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-;i>=;i--) {
while(m>k && cross(ch[m-]-ch[m-],p[i]-ch[m-])<=) m--;
ch[m++]=p[i];
}
if(n>) m--;
ch.resize(m);
return ch;
} int IsPointinPolygon(Pt p,vector<Pt>& poly) {
int wn=;
int n=poly.size();
for(int i=;i<n;i++) {
Pt& p1=poly[i];
Pt& p2=poly[(i+)%n];
if(p1==p || p2==p || OnSeg(p,p1,p2)) return -;
int k=dcmp(cross(p2-p1,p-p1));
int d1=dcmp(p1.y-p.y);
int d2=dcmp(p2.y-p.y);
if(k> && d1<= && d2>) wn++;
if(k< && d2<= && d1>) wn--;
}
if(wn!=) return ;
return ;
}
bool ConvexPolygonDisjoint(vector<Pt> ch1,vector<Pt> ch2) {
int c1=ch1.size() , c2=ch2.size();
for(int i=;i<c1;i++)
if(IsPointinPolygon(ch1[i],ch2)!=) return false;
for(int i=;i<c2;i++)
if(IsPointinPolygon(ch2[i],ch1)!=) return false;
for(int i=;i<c1;i++)
for(int j=;j<c2;j++)
if(SegIntersection(ch1[i],ch1[(i+)%c1],ch2[j],ch2[(j+)%c2])) return false;
return true;
} int main() {
int n,m;
while(scanf("%d%d",&n,&m)== && n && m) {
vector<Pt> P1,P2;
double x,y;
for(int i=;i<n;i++) {
scanf("%lf%lf",&x,&y);
P1.push_back(Pt(x,y));
}
for(int i=;i<m;i++) {
scanf("%lf%lf",&x,&y);
P2.push_back(Pt(x,y));
}
if(ConvexPolygonDisjoint(ConvexHull(P1),ConvexHull(P2)))
puts("Yes"); else puts("No");
}
return ;
}
UVA 10256 The Great Divide (凸包,多边形的位置关系)的更多相关文章
- UVA 10256 The Great Divide(凸包划分)
The Great Divide Input: standard input Output: standard output Time Limit: 8 seconds Memory Limit: 3 ...
- Cupid's Arrow---hdu1756(判断点与多边形的位置关系 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1756 题意:中文题,套模板即可: /* 射线法:判断一个点是在多边形内部,边上还是在外部,时间复杂度为 ...
- LightOj1190 - Sleepwalking(判断点与多边形的位置关系--射线法模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1190 题意:给你一个多边形含有n个点:然后又m个查询,每次判断点(x, y)是否在多边 ...
- ZOJ1081 Points Within 点和多边形的位置关系
ZOJ1081 给一个点和一个多边形 判断点在多边形内(边上)还是在多边形外 在多边形外的点引一条射线必然穿过多边形的两条边 而在多边形内的点则不一定. 当然凹多边形有特殊情况 但是总能找到对应位置关 ...
- UVa 10256 - The Great Divide 判断凸包相交
模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ...
- UVa 10256 (判断两个凸包相离) The Great Divide
题意: 给出n个红点,m个蓝点.问是否存在一条直线使得红点和蓝点分别分布在直线的两侧,这些点不能再直线上. 分析: 求出两种点的凸包,如果两个凸包相离的话,则存在这样一条直线. 判断凸包相离需要判断这 ...
- UVa 10256 The Great Divide,推断两个凸包是否相离
先从给出的两个点集中分别计算出两个凸包, 然后推断两个凸包是否相离. #include<cstdio> #include<vector> #include<cmath&g ...
- UVA 10256 The Great Divide(点在多边形内)
The Great Divid [题目链接]The Great Divid [题目类型]点在多边形内 &题解: 蓝书274, 感觉我的代码和刘汝佳的没啥区别,可是我的就是wa,所以贴一发刘汝佳 ...
- uva 10256 The Great Divide
题意:给定两个点集,一个红点集,另一个蓝点集,询问,能否找到一条直线能,使得任取一个红点和蓝点都在直线异侧. 思路:划分成两个凸包,一个红包,一个蓝包.两个凸包不相交不重合. 1.任取一个凸包中的点不 ...
随机推荐
- C++ map插入(insert)数据返回值
例子: typedef boost::unordered_map<int, int> UserOnlineMap; UserOnlineMap userOnlineMap_; std::p ...
- (转)iOS学习之 plist文件的读写
在做iOS开发时,经常用到到plist文件, 那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plist ...
- ellang 中进程异步通信中的信箱与保序
erlang 进程通讯中 执行到 receive 语句时 如果信箱没有消息可以匹配时会暂停等待消息. go() -> register(echo, spawn(test_pid,loop,[]) ...
- WebStorm快捷键收集
1.webstorm快捷键: IntelliJ-Idea 的快捷键 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*…*/ ) Shift+F6 重构-重命名 Ctrl+X 删除行 C ...
- php重载
重载 PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法.我们是通过 魔术方法(magic methods)来实现的. 当调用当前环 ...
- php 接口 implements 使用
主要对类名,类所拥有的方法,以及所传参数起约束和规范做用,感觉跟php abstract 抽象类又有点像. 一,接口的定义和调用 <?php interface face1 { const pa ...
- mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句
http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases; ...
- linux常用命令搜索
解压tar - xzvf webcmp.tar.gz /目的目录 压缩tar - czvf webcmp.tar.gz /压缩源文件 发包命令 cd /cap/sc_bossdata_20140516 ...
- WebApp开发:ajax请求跨域问题的解决
服务端:PHP 客户端:Andorid, HTML5, jQuery, ajax 现象:本想通过jQuery的ajax功能从服务器取回数据存到手机的缓存里,结果总是错误,后来想到可能是跨域问题,所以查 ...
- 最浅显、易懂的Linux 硬链接与软链接的理解
正文: Linux上的文件可以这么理解:文件-->文件名.文件是一个Object,也就是磁盘上的二进制数据.一个文件可以有多个文件名,平时我们都是通过文件名访问文件Object. 这样,硬链接可 ...