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 ...
随机推荐
- [No0000102]JavaScript-基础课程2
var bob = new Object(); bob.age = ; // 这一次我们已经添加了一个方法,setAge bob.setAge = function (newAge){ bob.age ...
- tensorflow 的tf.split函数的用法
将张量进行切分 tf.split( value, num_or_size_splits, axis=0, num=None, name='split' ) value: 待切分的张量 num_or_s ...
- toggle显示与隐藏切换
jQuery中显示与隐藏切换toggle方法 show与hide是一对互斥的方法.需要对元素进行显示隐藏的互斥切换,通常情况是需要先判断元素的display状态,然后调用其对应的处理方法.比如显示的元 ...
- RENAME方法进行分区改造
1.新建零时表 set time on timing on create table RPTUSER.RPT_TMP_10086BDL_bak ( HANDLEDATE DATE, TEL_CALLI ...
- oracle partition table 分区表详解
Oracle partition table 分区表详解 分区表就是通过使用分区技术,将一张大表,拆分成多个表分区(独立的segment),从而提升数据访问的性能,以及日常的可维护性.分区表中,每个分 ...
- 新建oracle用户
1.oracle 账户登录linux:2.如果存在多个切换实例:命令:export ORACLE_SID=实例名:如:export ORACLE_SID=utf81863.切换至管理员账户:sqlpl ...
- 如何使用List<HashMap<String, String>>详细讲解
场景:要循环界面Table数据源与导出Excel数据源作对比. 说明: List<HashMap<String,String>> List中每一项都是一个HashMap Ha ...
- JavaScript深度克隆
深度克隆函数: function deepClone(obj){ var str = ""; var newobj = obj.constructor === Array ? [] ...
- 【JMeter】【性能测试】服务器性能监控
下载插件 1.访问网址http://jmeter-plugins.org/downloads/all/,下载三个文件.其中JMeterPlugins-Standard和JMeterPlugins-Ex ...
- 【托业】【新托业TOEIC新题型真题】学习笔记3-题库二->P5-6
--------------------------------------单词-------------------------------------- oppose vt. 反对:对抗,抗争 v ...