Uva 10256 The Great Divide(凸包)
来源:互联网 发布:linux 查看光纤卡 编辑:程序博客网 时间:2024/06/05 14:20
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1197
思路:
分离两种点,即为分离两种点组成的两个凸包:
1.任取A凸包中的一点,判断是否在B凸包中;任取B凸包中的一点,判断是否在A凸包中;
2.任取A凸包上一条线段,判断是否与B凸包中线段相交;任取B凸包上一条线段,判断是否与A凸包中线段相交。
#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define debugusing namespace std;struct Point{ double x,y; Point(double x=0.0,double y=0.0):x(x),y(y) {} void read() { scanf("%lf%lf",&x,&y); }};typedef Point Vector;int dcmp(double x){ if(fabs(x)<0) return 0; else return x<0?-1:1;}bool operator < (Point A,Point B){ if(A.x==B.x) return A.y<A.y; else return A.x<B.x;}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 SegmentProperIntersection(Point a1,Point a2,Point b1,Point 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)<0&&dcmp(c3)*dcmp(c4)<0;}bool isPointOnSegment(Point p,Point a1,Point a2){ return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;}int ConvexHull(Point* p,int n,Point* ch){ sort(p,p+n); int m=0; 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--; return m;}int isPointInPolygon(Point p,Point* poly,int n){ int wn=0; for(int i=0; i<n; i++) { if(isPointOnSegment(p,poly[i],poly[(i+1)%n])) return -1; int k=dcmp(Cross(poly[(i+1)%n]-poly[i],p-poly[i])); int d1=dcmp(poly[i].y-p.y); int d2=dcmp(poly[(i+1)%n].y-p.y); if(k>0&&d1<=0&&d2>0) wn++; if(k<0&&d2<=0&&d1>0) wn--; } if(wn!=0) return 1; return 0;}const int maxn=500+50;int m,c;int num1,num2;Point p1[maxn],p2[maxn];Point convex1[maxn],convex2[maxn];int checkIn(){ for(int i=0; i<num1; i++) { if(isPointInPolygon(convex1[i],convex2,num2)) { return 1; } } for(int i=0; i<num2; i++) { if(isPointInPolygon(convex2[i],convex1,num1)) { return 1; } } return 0;}int checkIntersection(){ for(int i=0; i<num1; i++) { for(int j=0; j<num2; j++) { if(SegmentProperIntersection(convex1[i],convex1[(i+1)%num1],convex2[j],convex2[(j+1)%num2])) { return 1; } } } return 0;}int main(){#ifdef debu freopen("in.txt","r",stdin);#endif // debug while(scanf("%d%d",&m,&c)!=EOF&&(m+c)) { for(int i=0; i<m; i++) p1[i].read(); for(int i=0; i<c; i++) p2[i].read(); num1=ConvexHull(p1,m,convex1); num2=ConvexHull(p2,c,convex2); if(!checkIn()&&!checkIntersection()) { printf("Yes\n"); } else { printf("No\n"); } } return 0;}
阅读全文
0 0
- UVA 10256 The Great Divide 凸包
- UVA 10256 The Great Divide 凸包 .
- uva 10256 - The Great Divide(凸包)
- Uva 10256 The Great Divide(凸包)
- UVa 10256 The Great Divide 凸包, 凸包分离
- UVA 10256 The Great Divide(凸包应用)
- UVA 10256 The Great Divide
- UVA 10256 - The Great Divide
- UVA 10256 The Great Divide
- UVA 10256 The Great Divide
- uva 10256 The Great Divide
- UVa 10256 The Great Divide,判断两个凸包是否相离
- uva10256 The Great Divide(凸包+判断)
- UVA 10256 The Great Divide(凸包应用 即凸包+线段相交判定+点是否在凸包内判断)
- uva10256 The Great Divide
- Fixing the Great Wall UVA
- Fixing the Great Wall UVA
- uva 1045 - The Great Wall Game(KM)
- 产品工作框架与流程详解
- Thinkphp 多表查询 join table用法 同名字段查询
- js双色球
- Spark快速大数据分析——读书笔记
- 优先队列优化Dijkstra算法
- Uva 10256 The Great Divide(凸包)
- 第二篇:Java实现多种单例模式SingletonPattern
- Java并发
- 字符串匹配算法-KMP
- Hibernate抛出异常org.hibernate.exception.SQLGrammarException: could not extract ResultSet
- Struts-DispatchAction
- 《Unix网络编程》卷1:套接字联网API(第3版):守护进程和inetd超级服务器、高级I/O、Unix域协议
- 第三篇:设计模式六大原则: 一个萝卜一个坑 -- 单一职责原则
- C++11:线程