POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳
题意: 给你两个凸包,求其最短距离。
解法: POJ 我真的是弄不懂了,也不说一声点就是按顺时针给出的,不用调整点顺序。 还是说数据水了,没出乱给点或给逆时针点的数据呢。。我直接默认顺时针给的点居然A了,但是我把给的点求个逆时针凸包,然后再反转一下时针顺序,又WA了。这其中不知道有什么玄机。。
求凸包最短距离还是用旋转卡壳的方法,这里采用的是网上给出的一种方法:
英文版: http://cgm.cs.mcgill.ca/~orm/mind2p.html
中文翻译版: http://www.cnblogs.com/bless/archive/2008/08/06/1262438.html
输入的两个凸包须是顺时针。
分别以一个为主卡另外一个,两次取最小值即可。
算法就不分析了, 画个图理解一下就知道了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
#define eps 1e-8
using namespace std; struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
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); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
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; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); } double DistanceToSeg(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);
if(dcmp(Dot(v1, v3)) > ) return Length(v3);
return fabs(Cross(v1, v2)) / Length(v1);
}
double SegDistancetoSeg(Point A,Point B,Point C,Point D) {
return min(min(DistanceToSeg(C,A,B),DistanceToSeg(D,A,B)),min(DistanceToSeg(A,C,D),DistanceToSeg(B,C,D)));
}
Point DisP(Point A,Point B) { return Length(B-A); } double MinDisOfTwoConvexHull(Point P[],int n,Point Q[],int m) {
int Pymin = , Qymax = , i,j;
for(i=;i<n;i++) if(dcmp(P[i].y-P[Pymin].y) < ) Pymin = i;
for(i=;i<m;i++) if(dcmp(Q[i].y-Q[Qymax].y) > ) Qymax = i;
P[n] = P[], Q[m] = Q[];
double Mindis = 1e90, Tmp;
for(i=;i<n;i++) {
while(dcmp(Tmp = Cross(P[Pymin+]-P[Pymin],Q[Qymax+]-P[Pymin])-Cross(P[Pymin+]-P[Pymin],Q[Qymax]-P[Pymin])) > )
Qymax = (Qymax+)%m;
if(dcmp(Tmp) < ) Mindis = min(Mindis,DistanceToSeg(Q[Qymax],P[Pymin],P[Pymin+]));
else Mindis = min(Mindis,SegDistancetoSeg(P[Pymin],P[Pymin+],Q[Qymax],Q[Qymax+]));
Pymin = (Pymin+)%n;
}
return Mindis;
} Point P[],nP[],Q[],nQ[]; int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=EOF && n+m)
{
for(i=;i<n;i++) P[i].input();
for(i=;i<m;i++) Q[i].input();
printf("%.5f\n",min(MinDisOfTwoConvexHull(P,n,Q,m),MinDisOfTwoConvexHull(Q,m,P,n)));
}
return ;
}
求凸包,反转,WA。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
#define eps 1e-8
using namespace std; struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
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 dcmp(a.x-b.x)< || (dcmp(a.x-b.x)== && dcmp(a.y-b.y)<); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
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; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); } double DistanceToSeg(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);
if(dcmp(Dot(v1, v3)) > ) return Length(v3);
return fabs(Cross(v1, v2)) / Length(v1);
}
double SegDistancetoSeg(Point A,Point B,Point C,Point D) {
return min(min(DistanceToSeg(C,A,B),DistanceToSeg(D,A,B)),min(DistanceToSeg(A,C,D),DistanceToSeg(B,C,D)));
}
Point DisP(Point A,Point B) { return Length(B-A); }
bool SegmentIntersection(Point A,Point B,Point C,Point D) {
return max(A.x,B.x) >= min(C.x,D.x) &&
max(C.x,D.x) >= min(A.x,B.x) &&
max(A.y,B.y) >= min(C.y,D.y) &&
max(C.y,D.y) >= min(A.y,B.y) &&
dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= &&
dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= ;
}
void SegIntersectionPoint(Point& P,Point a,Point b,Point c,Point d) { //需保证ab,cd相交
P.x = (Cross(d-a,b-a)*c.x - Cross(c-a,b-a)*d.x)/(Cross(d-a,b-a)-Cross(c-a,b-a));
P.y = (Cross(d-a,b-a)*c.y - Cross(c-a,b-a)*d.y)/(Cross(d-a,b-a)-Cross(c-a,b-a));
}
void CounterClockwiseToClockWise(Point* p,Point *np,int n){
np[] = p[];
for(int i=;i<n;i++) np[i] = p[n-i];
}
int ConvexHull(Point* p, int n, Point* ch)
{
sort(p,p+n);
int m = ;
for(int i=;i<n;i++) {
while(m > && dcmp(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 && dcmp(Cross(ch[m-]-ch[m-], p[i]-ch[m-])) <= ) m--;
ch[m++] = p[i];
}
if(n > ) m--;
return m;
}
double MinDisOfTwoConvexHull(Point* P,int n,Point* Q,int m) {
int Pymin = , Qymax = , i,j;
for(i=;i<n;i++) if(dcmp(P[i].y-P[Pymin].y) < ) Pymin = i;
for(i=;i<m;i++) if(dcmp(Q[i].y-Q[Qymax].y) > ) Qymax = i;
P[n] = P[], Q[m] = Q[];
double Mindis = 1e90, Tmp;
for(i=;i<n;i++) {
while(dcmp(Tmp = Cross(P[Pymin+]-P[Pymin],Q[Qymax+]-P[Pymin])-Cross(P[Pymin+]-P[Pymin],Q[Qymax]-P[Pymin])) > )
Qymax = (Qymax+)%m;
if(dcmp(Tmp) < ) Mindis = min(Mindis,DistanceToSeg(Q[Qymax],P[Pymin],P[Pymin+]));
else Mindis = min(Mindis,SegDistancetoSeg(P[Pymin],P[Pymin+],Q[Qymax],Q[Qymax+]));
Pymin = (Pymin+)%n;
}
return Mindis;
} Point P[],nP[],Q[],nQ[]; int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=EOF && n+m)
{
for(i=;i<n;i++) P[i].input();
for(i=;i<m;i++) Q[i].input();
ConvexHull(P,n,nP);
CounterClockwiseToClockWise(nP,P,n);
ConvexHull(Q,m,nQ);
CounterClockwiseToClockWise(nQ,Q,m);
printf("%.5f\n",min(MinDisOfTwoConvexHull(P,n,Q,m),MinDisOfTwoConvexHull(Q,m,P,n)));
}
return ;
}
POJ 3608 Bridge Across Islands --凸包间距离,旋转卡壳的更多相关文章
- POJ - 3608 Bridge Across Islands【旋转卡壳】及一些有趣现象
给两个凸包,求这两个凸包间最短距离 旋转卡壳的基础题 因为是初学旋转卡壳,所以找了别人的代码进行观摩..然而发现很有意思的现象 比如说这个代码(只截取了关键部分) double solve(Point ...
- ●POJ 3608 Bridge Across Islands
题链: http://poj.org/problem?id=3608 题解: 计算几何,求两个凸包间的最小距离,旋转卡壳 两个凸包间的距离,无非下面三种情况: 所以可以基于旋转卡壳的思想,去求最小距离 ...
- poj 3608 Bridge Across Islands
题目:计算两个不相交凸多边形间的最小距离. 分析:计算几何.凸包.旋转卡壳.分别求出凸包,利用旋转卡壳求出对踵点对,枚举距离即可. 注意:1.利用向量法判断旋转,而不是计算角度:避免精度问题和TLE. ...
- POJ 3608 Bridge Across Islands(旋转卡壳,两凸包最短距离)
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7202 Accepted: ...
- POJ 3608 Bridge Across Islands [旋转卡壳]
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10455 Accepted: ...
- POJ 3608 Bridge Across Islands (旋转卡壳)
[题目链接] http://poj.org/problem?id=3608 [题目大意] 求出两个凸包之间的最短距离 [题解] 我们先找到一个凸包的上顶点和一个凸包的下定点,以这两个点为起点向下一个点 ...
- POJ 3608 Bridge Across Islands(计算几何の旋转卡壳)
Description Thousands of thousands years ago there was a small kingdom located in the middle of the ...
- poj 3608 Bridge Across Islands 两凸包间最近距离
/** 旋转卡壳,, **/ #include <iostream> #include <algorithm> #include <cmath> #include ...
- poj 3068 Bridge Across Islands
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11196 Accepted: ...
随机推荐
- JAVASCRIPT实现简单计算器
最终效果如下图-2,有bug:就是整数后点击%号结果正确,如果小数后面点击%的话结果就错误!其他都正常,求指点:input的value是string类型的,在JS中改如何正确处理下图-1中的if部分? ...
- 一个解决表单中的文字和文本区域(textarea)上对齐的方法
在进行表单布局的时候通常会遇到这样的情况 文本和textarea标签是底部对齐的 <p><em>邮箱</em><textarea style='height: ...
- 关于mapcontrol和pagelayoutcontrol切换时闪退
今天遇到一个很奇怪的的现象,在tabcontrol里切换到pagelayout时,程序会闪退,试了下之前的程序,没有问题,去网上搜了一下,也没人有这样的问题,然后就开始实验,添加一个控件,运行一次,最 ...
- SharePoint 2010 匿名访问开启后不能访问Allitems.aspx或DisplayForm.aspx
Body: Full Credit goes to Pet Stilgoe: http://www.petestilgoe.com/2010/02/allowed-anonymous-access-o ...
- 【转】C++标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...
- Android上的MVP:如何组织显示层的内容
MVP(Model View Presenter)模式是著名的MVC(Model View Controller)模式的一个演化版本,目前它在Android应用开发中越来越重要了,大家也都在讨论关于M ...
- 《The Linux Command Line》 读书笔记04 Linux用户以及权限相关命令
Linux用户以及权限相关命令 查看身份 id:Display user identity. 这个命令的输出会显示uid,gid和用户所属的组. uid即user ID,这是账户创建时被赋予的. gi ...
- xmpp整理笔记:聊天信息的发送与显示
任何一个信息的发送都需要关注两个部分,信息的发出,和信息在界面中的显示 往期回顾: xmpp整理笔记:环境的快速配置(附安装包) http://www.cnblogs.com/dsxniubilit ...
- Android上传图片到PHP服务器并且支持浏览器上传文件(word、图片、音乐等)
暑假已经过了一半了,这才完成计划当中的第二个任务.虽然进度是慢了点.但也算是暑假的收获吧.下面我就把我学习当中的收获记录在此. 还是跟以往一样,先上图片. 操作的步骤:打开程序---->选择上传 ...
- 免费真机调试 -- Xcode7
刚新安装了Xcode7 Version 7.1 beta , 据说这个版本可以免费真机调试,于是用了一个新的AppID测试了,发现真的可以免费真机调试了呢!新的appId账号,没有支付每年的99美刀, ...