UVA 10256 The Great Divide(点在多边形内)
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(点在多边形内)的更多相关文章
- UVA 10256  The Great Divide (凸包,多边形的位置关系)
		题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34148 [思路] 凸包 求出红蓝点的凸包,剩下的问题就是判断两个凸 ... 
- UVa 10256 - The Great Divide 判断凸包相交
		模板敲错了于是WA了好几遍…… 判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No. 训练指南上的分析: 1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交.如果相交( ... 
- UVA 10256 The Great Divide(凸包划分)
		The Great Divide Input: standard input Output: standard output Time Limit: 8 seconds Memory Limit: 3 ... 
- uva 10256 The Great Divide
		题意:给定两个点集,一个红点集,另一个蓝点集,询问,能否找到一条直线能,使得任取一个红点和蓝点都在直线异侧. 思路:划分成两个凸包,一个红包,一个蓝包.两个凸包不相交不重合. 1.任取一个凸包中的点不 ... 
- UVa 10256 The Great Divide,推断两个凸包是否相离
		先从给出的两个点集中分别计算出两个凸包, 然后推断两个凸包是否相离. #include<cstdio> #include<vector> #include<cmath&g ... 
- UVA - 10375 Choose and divide[唯一分解定理]
		UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ... 
- 【暑假】[数学]UVa 10375 Choose and divide
		UVa 10375 Choose and divide 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19601 思路 ... 
- PHP 判断点是否在多边形内
		如何判断一个点是否在一个多边形内,何时会用到这个场景. 我们就模拟一个真是场景.我们公司是快递公司,在本地区域有6个分点.每个分点有3-5个工人负责附近的快递派遣发送,所以根据每个点的服务区域我们就能 ... 
- 结合谷歌地图多边形(polygon)与Sql Server 2008的空间数据类型计算某个点是否在多边形内的注意事项
		首先在利用 GEOGRAPHY::STPolyFromText(@GeoStr, 4326) 这样的函数把字符串转换为Geography类型时,字符串里经纬度的顺序是 “经度[空格]纬度”,即“lon ... 
随机推荐
- [No0000125]WCF安全体系
			WCF的安全体系主要包括三个方面:传输安全(Transfer Security).授权或者访问控制(Authorization OR Access Control)以及审核(Auditing).而传输 ... 
- HTML5:表单提交
			不加CSS.JavaScrips的HTML表单提交简单代码 <!DOCTYPE html> <html lang="en"> <head> &l ... 
- springmvc整合spring+mybatis出现的404或者报找不到这个类的时候。
			1.文件配置没有问题的时候看这个看看.输出目录改成这个.要有这里所有的目录才行 
- C#查找字符串位置
			int i=zifuchuan.IndexOf(","); int n=(zifuchuan.SubString(i+1)).IndexOf(","); int ... 
- tomcat在Eclipse中和idea中的使用
			在eclipse中的使用 下载 http://tomcat.apache.org/ 部署项目到tomcat 常见问题 访问时如何出掉项目名 中文乱码问题 1.浏览器编码问题,修改浏览器的编码 2.js ... 
- beego的https和http同时启用
			2017/07/19 14:01:03 [I] [asm_amd64.s:2197] http server Running on http://:8080 2017/07/19 14:01:03 [ ... 
- es6学习一 promise上
			简单来说promise在异步操作上提供可读性.(原来es5的异步操作可读性实在太糟糕了,如下图) 瞬间眼前百万只奔腾的马,只不过这种马有个别名,羊驼. 一.创建形式 1. 使用的基本形式: let p ... 
- 自定义指令(v-check、v-focus)的方法有哪些?它有哪些钩子函数?还有哪些钩子函数参数?
			全局定义指令:在vue对象的directive方法里面有两个参数,一个是指令名称,另一个是函数.组件内定义指令:directives: 钩子函数:bind(绑定事件触发).inserted(节点插入的 ... 
- FastDFS的使用
			1.FastDFS 1.1. 什么是FastDFS? FastDFS是用c语言编写的一款开源的分布式文件系统.FastDFS为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用 ... 
- python练习题-day2
			1.判断下列逻辑语句的True,False 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 True ... 
