UVa 10256 - The Great Divide 判断凸包相交
模板敲错了于是WA了好几遍……
判断由红点和蓝点分别组成的两个凸包是否相离,是输出Yes,否输出No。
训练指南上的分析:
1.任取红凸包上的一条线段和蓝凸包上的一条线段,判断二者是否相交。如果相交(不一定是规范相交,有公共点就算相交),则无解
2.任取一个红点,判断是否在蓝凸包内。如果是,则无解。蓝点红凸包同理。
其中任何一个凸包退化成点或者线段时需要特判。
其实只需要把上面两个判断顺序颠倒一下,就可以不需要特判。
先判断点是否在凸包内,因为这个考虑了点在凸包边界上的情况,所以后面判凸包线段是否相交时直接用规范相交判断即可。
此时特殊情况包含在了上两种情况中,因此不需要特判。
#include <cstdio>
#include <cmath>
#include <algorithm> using namespace std; const int MAXN = ; const double eps = 1e-; struct Point
{
double x, y;
Point( double x = , double y = ):x(x), y(y) { }
}; typedef Point Vector; Vector operator+( Vector A, Vector B ) //向量加
{
return Vector( A.x + B.x, A.y + B.y );
} Vector operator-( Vector A, Vector B ) //向量减
{
return Vector( A.x - B.x, A.y - B.y );
} Vector operator*( Vector A, double p ) //向量数乘
{
return Vector( A.x * p, A.y * p );
} Vector operator/( Vector A, double p ) //向量数除
{
return Vector( A.x / p, A.y / p );
} bool operator<( const Point& A, const Point& B ) //两点比较
{
return A.x < B.x || ( A.x == B.x && A.y < B.y );
} int dcmp( double x ) //控制精度
{
if ( fabs(x) < eps ) return ;
else return x < ? - : ;
} bool operator==( const Point& a, const Point& b ) //两点相等
{
return dcmp( a.x - b.x ) == && dcmp( a.y - b.y ) == ;
} double Dot( Vector A, Vector B ) //向量点乘
{
return A.x * B.x + A.y * B.y;
} double Length( Vector A ) //向量模
{
return sqrt( Dot( A, A ) );
} double Angle( Vector A, Vector B ) //向量夹角
{
return acos( Dot(A, B) / Length(A) / Length(B) );
} double Cross( Vector A, Vector B ) //向量叉积
{
return A.x * B.y - A.y * B.x;
} double Area2( Point A, Point B, Point C ) //向量有向面积
{
return Cross( B - A, C - A );
} Vector Rotate( Vector A, double rad ) //向量旋转
{
return Vector( A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad) );
} Vector Normal( Vector A ) //向量单位法向量
{
double L = Length(A);
return Vector( -A.y / L, A.x / L );
} Point GetLineIntersection( Point P, Vector v, Point Q, Vector w ) //两直线交点
{
Vector u = P - Q;
double t = Cross( w, u ) / Cross( v, w );
return P + v * t;
} double DistanceToLine( Point P, Point A, Point B ) //点到直线的距离
{
Vector v1 = B - A, v2 = P - A;
return fabs( Cross( v1, v2 ) ) / Length(v1);
} double DistanceToSegment( Point P, Point A, Point B ) //点到线段的距离
{
if ( A == B ) return Length( P - A );
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if ( dcmp( Dot(v1, v2) ) < ) return Length(v2);
else if ( dcmp( Dot(v1, v3) ) > ) return Length(v3);
else return fabs( Cross( v1, v2 ) ) / Length(v1);
} Point GetLineProjection( Point P, Point A, Point B ) // 点在直线上的投影
{
Vector v = B - A;
return A + v*( Dot(v, P - A) / Dot( v, v ) );
} 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) < && dcmp(c3) * dcmp(c4) < ;
} bool OnSegment( Point p, Point a1, Point a2 ) //点在线段上,不包含端点
{
return dcmp( Cross(a1 - p, a2 - p) ) == && dcmp( Dot( a1 - p, a2 - p ) ) < ;
} double toRad( double deg ) //角度转弧度
{
return deg / 180.0 * acos( -1.0 );
} int ConvexHull( Point *p, int n, Point *ch ) //求凸包
{
sort( p, p + n );
n = unique( p, p + n ) - p;
int m = ;
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;
return m;
} int isPointInPolygon( Point p, Point *poly, int n ) //判断一点是否在凸包内
{
int wn = ; for ( int i = ; i < n; ++i )
{
Point& p1 = poly[i], p2 = poly[ (i + )%n ];
if ( p == p1 || p == p2 || OnSegment( 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 ; //外部
} double PolygonArea( Point *p, int n ) //多边形有向面积
{
double area = ;
for ( int i = ; i < n - ; ++i )
area += Cross( p[i] - p[], p[i + ] - p[] );
return area / 2.0;
} bool checkConvexHullIntersection( Point *a, Point *b, int na, int nb )
{
for ( int i = ; i < na; ++i )
if ( isPointInPolygon( a[i], b, nb ) ) return true; for ( int i = ; i < nb; ++i )
if ( isPointInPolygon( b[i], a, na ) ) return true; for ( int i = ; i < na; ++i )
for ( int j = ; j < nb; ++j )
if ( SegmentProperIntersection(a[i], a[ (i + ) % na ], b[j], b[ (j + ) % nb ] ) ) return true; return false;
} Point M[MAXN], chM[MAXN];
Point C[MAXN], chC[MAXN]; int main()
{
int Mn, Cn;
while ( scanf( "%d%d", &Mn, &Cn ), Mn || Cn )
{
for ( int i = ; i < Mn; ++i )
scanf( "%lf%lf", &M[i].x, &M[i].y ); for ( int i = ; i < Cn; ++i )
scanf( "%lf%lf", &C[i].x, &C[i].y ); int Mcnt = ConvexHull( M, Mn, chM );
int Ccnt = ConvexHull( C, Cn, chC ); if ( checkConvexHullIntersection( chM, chC, Mcnt, Ccnt ) ) puts("No");
else puts("Yes");
}
return ;
}
UVa 10256 - The Great Divide 判断凸包相交的更多相关文章
- UVA 10256 The Great Divide (凸包,多边形的位置关系)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34148 [思路] 凸包 求出红蓝点的凸包,剩下的问题就是判断两个凸 ...
- POJ 3805 Separate Points (判断凸包相交)
题目链接:POJ 3805 Problem Description Numbers of black and white points are placed on a plane. Let's ima ...
- HDU 6590 Code (判断凸包相交)
2019 杭电多校 1 1013 题目链接:HDU 6590 比赛链接:2019 Multi-University Training Contest 1 Problem Description Aft ...
- 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,推断两个凸包是否相离
先从给出的两个点集中分别计算出两个凸包, 然后推断两个凸包是否相离. #include<cstdio> #include<vector> #include<cmath&g ...
- uva 10256 The Great Divide
题意:给定两个点集,一个红点集,另一个蓝点集,询问,能否找到一条直线能,使得任取一个红点和蓝点都在直线异侧. 思路:划分成两个凸包,一个红包,一个蓝包.两个凸包不相交不重合. 1.任取一个凸包中的点不 ...
- UVA 10256 The Great Divide(点在多边形内)
The Great Divid [题目链接]The Great Divid [题目类型]点在多边形内 &题解: 蓝书274, 感觉我的代码和刘汝佳的没啥区别,可是我的就是wa,所以贴一发刘汝佳 ...
- UVa 10256 (判断两个凸包相离) The Great Divide
题意: 给出n个红点,m个蓝点.问是否存在一条直线使得红点和蓝点分别分布在直线的两侧,这些点不能再直线上. 分析: 求出两种点的凸包,如果两个凸包相离的话,则存在这样一条直线. 判断凸包相离需要判断这 ...
- UVALive7461 - Separating Pebbles 判断两个凸包相交
//UVALive7461 - Separating Pebbles 判断两个凸包相交 #include <bits/stdc++.h> using namespace std; #def ...
随机推荐
- hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意:一个N*M的矩阵,改变一个格子,本身+2,四周+1.同时mod 3;问操作多少次,矩阵变为全0.输出 ...
- ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
OS: centos 6.3DB: 5.5.14 测试创建yoon测试表,没有主键,没有索引,基础数据内容如下: mysql> select * from yoon;+----+-------- ...
- oracle作业
http://blog.csdn.net/hao_ds/article/details/38382931 oracle作业各种参数的详细介绍
- php集成开发环境IDE
ZendStudio EclipsePHP PhpStorm NetBeans
- CentOS-6.5安装配置JDK-7和JDK-8
安装说明 系统环境:centos-6.5 软件:jdk-7-linux-x64.rpm , jdk-8u5-linux-i586.tar.gz 下载地址:http://www.oracle.com/ ...
- bnuoj 16493 Just Pour the Water(矩阵快速幂)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...
- java集合类(六)About Queue
接上篇“java集合类(五)About Map” 终于来到了java集合类的尾声,太兴奋了,不是因为可以休息一阵了,而是因为又到了开启新知识的时刻,大家一起加油打气!!Come on...Fighti ...
- js获取对象、数组的实际长度,元素实际个数
/*获取对象.数组的长度.元素个数 *@param obj 要计算长度的元素,可以为object.array.string */ function count(obj){ var objType = ...
- 1965: [Ahoi2005]SHUFFLE 洗牌 - BZOJ
Description 为了表彰小联为Samuel星球的探险所做出的贡献,小联被邀请参加Samuel星球近距离载人探险活动. 由于Samuel星球相当遥远,科学家们要在飞船中度过相当长的一段时间,小联 ...
- 在一个字符串(1<=字符串长度<=10000,全部由大小写字母组成)中找到第一个只出现一次的字符,并返回它的位置
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...