The Great Divid

【题目链接】The Great Divid

【题目类型】点在多边形内

&题解:

蓝书274, 感觉我的代码和刘汝佳的没啥区别,可是我的就是wa,所以贴一发刘汝佳的吧.

感觉这题最好的地方就是让我大致懂了点在多边形内的判断,写的好神奇,没有做一条直线,而是2个if判断就替代了这个,好腻害

&代码:

#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std; const double eps = 1e-10;
int dcmp(double x) {if(fabs(x)<eps) return 0; return x<0?-1:1;}
struct Point {
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
};
typedef Point Vector;
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
bool operator == (Point a,Point b) {return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}
bool operator < (Point a,Point b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);} bool SegmentProperIntersection(const Point& a1,const Point& a2,const Point& b1,const Point& b2) {
double c1=Cross(a2-a1 , b1-a1), c2=Cross(a2-a1 , b2-a1);
double c3=Cross(b2-b1 , a1-b1), c4=Cross(b2-b1 , a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
}
bool OnSegment(const Point& p,const Point& a1,const Point& a2) {
return dcmp(Cross(a1-p , a2-p))==0 && dcmp(Dot(a1-p , a2-p))<0;
} vector<Point> ConvexHull(vector<Point> p) {
sort(p.begin(),p.end());
p.erase(unique(p.begin(), p.end()), p.end());
int n=p.size();
int m=0;
vector<Point> ch(n+1);
for(int i=0; i<n; i++) {
while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2; i>=0; i--) {
while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
if(n>1) m--;
ch.resize(m);
return ch;
} int IsInPolygon(const Point& p,const vector<Point>& poly) {
int n = poly.size() , wn=0;
for(int i=0; i<n; i++) {
const Point& p1 = poly[i] , p2 = poly[(i+1)%n];
if(p1==p || p2==p || OnSegment(p, p1, p2)) return -1;
int k=dcmp(Cross(p2-p1 , p-p1));
int d1=dcmp(p1.y - p.y) , d2=dcmp(p2.y - p.y);
if(k>0 && d1<=0 && d2>0) wn++;
if(k<0 && d2<=0 && d1>0) wn--;
}
return wn?1:0;
}
bool ConvexPolygonDisjoint(const vector<Point> ch1,const vector<Point> ch2) {
int c1=ch1.size(), c2=ch2.size();
for(int i=0; i<c1; i++) {
if(IsInPolygon(ch1[i],ch2)) return false;
}
for(int i=0; i<c2; i++) {
if(IsInPolygon(ch2[i],ch1)) return false;
}
for(int i=0; i<c1; i++) {
for(int j=0; j<c2; j++) {
if(SegmentProperIntersection(ch1[i],ch1[(i+1)%c1],ch2[j],ch2[(j+1)%c2])) return false;
}
}
return true;
}
int main() {
freopen("e:1.in","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)==2&&n>0&&m>0) {
vector<Point> P1,P2;
double x,y;
for(int i=0; i<n; i++) {
scanf("%lf%lf",&x,&y);
P1.push_back(Point(x,y));
}
for(int i=0; i<m; i++) {
scanf("%lf%lf",&x,&y);
P2.push_back(Point(x,y));
}
if(ConvexPolygonDisjoint(ConvexHull(P1), ConvexHull(P2)))
printf("Yes\n");
else
printf("No\n");
}
}

UVA 10256 The Great Divide(点在多边形内)的更多相关文章

  1. UVA 10256 The Great Divide (凸包,多边形的位置关系)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34148 [思路] 凸包 求出红蓝点的凸包,剩下的问题就是判断两个凸 ...

  2. UVa 10256 - The Great Divide 判断凸包相交

    模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ...

  3. UVA 10256 The Great Divide(凸包划分)

    The Great Divide Input: standard input Output: standard output Time Limit: 8 seconds Memory Limit: 3 ...

  4. uva 10256 The Great Divide

    题意:给定两个点集,一个红点集,另一个蓝点集,询问,能否找到一条直线能,使得任取一个红点和蓝点都在直线异侧. 思路:划分成两个凸包,一个红包,一个蓝包.两个凸包不相交不重合. 1.任取一个凸包中的点不 ...

  5. UVa 10256 The Great Divide,推断两个凸包是否相离

    先从给出的两个点集中分别计算出两个凸包, 然后推断两个凸包是否相离. #include<cstdio> #include<vector> #include<cmath&g ...

  6. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  7. 【暑假】[数学]UVa 10375 Choose and divide

    UVa 10375 Choose and divide 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19601 思路 ...

  8. PHP 判断点是否在多边形内

    如何判断一个点是否在一个多边形内,何时会用到这个场景. 我们就模拟一个真是场景.我们公司是快递公司,在本地区域有6个分点.每个分点有3-5个工人负责附近的快递派遣发送,所以根据每个点的服务区域我们就能 ...

  9. 结合谷歌地图多边形(polygon)与Sql Server 2008的空间数据类型计算某个点是否在多边形内的注意事项

    首先在利用 GEOGRAPHY::STPolyFromText(@GeoStr, 4326) 这样的函数把字符串转换为Geography类型时,字符串里经纬度的顺序是 “经度[空格]纬度”,即“lon ...

随机推荐

  1. strlen函数的汇编实现分析

    [原创][老刘谈算法001]这位运算玩的真溜—strlen函数的汇编实现分析-『密码算法』-看雪安全论坛 https://bbs.pediy.com/thread-229243.htm

  2. 8 queen暴力解决法

    很容易看出来,在每一行和每一列上有且只能有一个皇后,因此较为复杂的判断就是对角线了.维基百科的页面上有一个非常暴力但是写起来非常简单的解法: 1 2 3 4 5 6 7 8 from itertool ...

  3. [daily][archlinux][rsync] rsync

    科普文档:https://wiki.archlinux.org/index.php/Rsync 之前改文件系统时,用过. 然而用的不太对,导致一部分文件的权限出了问题. [troubleshoot][ ...

  4. Copycat - command

    client.submit(new PutCommand("foo", "Hello world!")); ServerContext connection.h ...

  5. LeetCode 766 Toeplitz Matrix 解题报告

    题目要求 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...

  6. Appium入门(9)—— Appium API

    摘自:http://www.testclass.net/appium/appium-base-api-01/ 1.安装: installApp() driver.installApp("d: ...

  7. 20165213&20165225结对学习感想及创意照

    20165213&20165225结对学习感想及创意照 会JAVA的大学生活好小组 团队感悟: 1+1>2还是1+1<2? 上述两个观点实际没有对错之分,取决点在于个人见解. 相信 ...

  8. 陌生的 metaclass(转)

    add by zhj:这是我见过的对metaclass解释最清楚的文章了,例子很好,真是一例胜千言 原文:http://wiki.jikexueyuan.com/project/explore-pyt ...

  9. Lua: 给 Redis 用户的入门指导(转)

    add by zhj : Lua与Multi/EXEC的功能比较像,在执行过程中,redis不会执行其它命令,这就不会有并发访问的问题, 这是非常好的.但Multi/EXEC要求所有命令都是独立的,后 ...

  10. 转载:caffe中的Reshape层

    http://blog.csdn.net/terrenceyuu/article/details/76228317 #作用:在不改变数据的情况下,改变输入的维度 layer { name: " ...