POJ 3525 Most Distant Point from the Sea (banpingmianjiao)

本文详细介绍了如何使用二分查找法计算凸多边形中内切圆的最大半径,通过判断半平面交集的存在与否来确定内切圆半径的上下界。

Description

The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The most distant point is located in former Usuda Town, Nagano Prefecture, whose distance from the sea is 114.86 km.

In this problem, you are asked to write a program which, given a map of an island, finds the most distant point from the sea in the island, and reports its distance from the sea. In order to simplify the problem, we only consider maps representable by convex polygons.

Input

The input consists of multiple datasets. Each dataset represents a map of an island, which is a convex polygon. The format of a dataset is as follows.

n  
x1 y1
  
xn yn

Every input item in a dataset is a non-negative integer. Two input items in a line are separated by a space.

n in the first line is the number of vertices of the polygon, satisfying 3 ≤ n ≤ 100. Subsequent n lines are the x- and y-coordinates of the n vertices. Line segments (xiyi)–(xi+1yi+1) (1 ≤ i ≤ n − 1) and the line segment (xn,yn)–(x1y1) form the border of the polygon in counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions. All coordinate values are between 0 and 10000, inclusive.

You can assume that the polygon is simple, that is, its border never crosses or touches itself. As stated above, the given polygon is always a convex one.

The last dataset is followed by a line containing a single zero.

Output

For each dataset in the input, one line containing the distance of the most distant point from the sea should be output. An output line should not contain extra characters such as spaces. The answer should not have an error greater than 0.00001 (10−5). You may output any number of digits after the decimal point, provided that the above accuracy condition is satisfied.

Sample Input

4
0 0
10000 0
10000 10000
0 10000
3
0 0
10000 0
7000 1000
6
0 40
100 20
250 40
250 70
100 90
0 70
3
0 0
10000 10000
5000 5001
0

Sample Output

5000.000000
494.233641
34.542948
0.353553

这个题目限制了是一个凸多边形,于是多边形所在的范围就等价于各个边所在的直线划分成的半平面的交,如果是凹多边形的话显然就不能这样等价了。

首先,我们可以把问题转化为求凸多边形的半径最大的内切圆,同时,我们会发现,如果各个边向内收缩r的话,内切圆的半径就会减少r,当缩到半平面交恰好不存在时,内切圆的半径也就为0了,这时向内收缩的距离r自然就是内切圆的最大半径了。

于是我们只要二分内切圆的半径r作为各条边向内收缩的距离,然后判断这时半平面交是否为空集即可,如果为空则说明向内收缩的过头了,于是就要更新max,否则就更新min。

值得一提的是,当半平面交恰好不存在时(或者说恰好存在时),半平面交表示的就是半径最大的内切圆的圆心。

#include <iostream> #include <iomanip> #include <math.h> #include <string.h> #include <algorithm> using namespace std; #define eps 1e-8 struct Point {     double x,y;     Point(){}     Point(double x,double y):x(x),y(y){} }; struct Line {     Point P;     Point v;     double ang;     Line(){}     Line(Point P,Point v):P(P),v(v){ang=atan2(v.y,v.x);}     bool operator < (const Line&L)const     {         return ang<L.ang;     } }; Point operator + (Point A,Point B) {     return Point(A.x+B.x,A.y+B.y); } Point operator - (Point A,Point B) {     return Point(A.x-B.x,A.y-B.y); } Point operator * (Point A,double p) {     return Point(A.x*p,A.y*p); } double Dot(Point A,Point B) {     return A.x*B.x+A.y*B.y; } double Length(Point A) {     return sqrt(Dot(A,A)); } Point Normal(Point A) {     double L=Length(A);     return Point(-A.y/L,A.x/L); } double Cross(Point A,Point B) {     return A.x*B.y-A.y*B.x; } bool OnLeft(Line L,Point p)//点p在有向直线L的左边(线上不算) {     return Cross(L.v,p-L.P)>0; } Point GetIntersection(Line a,Line b)//两直线交点,假定直线唯一存在 {     Point u=a.P-b.P;     double t=Cross(b.v,u)/Cross(a.v,b.v);     return a.P+a.v*t; } int HalfplaneIntersection(Line *L,int n,Point *poly) {     sort(L,L+n);//按极角排序     int first,last;//双端队列的第一个元素和最后一个元素     Point *p=new Point[n];//p[i]为q[i]和q[i+1]的交点     Line *q=new Line[n];//双端队列     q[first=last=0]=L[0];//双端队列初始化为只有一个半平面L[0]     for(int i=1;i<n;i++)     {         while(first<last&&!OnLeft(L[i],p[last-1]))last--;         while(first<last&&!OnLeft(L[i],p[first]))first++;         q[++last]=L[i];         if(fabs(Cross(q[last].v,q[last-1].v))<eps)//两向量平行且同向,取内测的一个         {             last--;             if(OnLeft(q[last],L[i].P))q[last]=L[i];         }         if(first<last)p[last-1]=GetIntersection(q[last-1],q[last]);     }     while(first<last&&!OnLeft(q[first],p[last-1]))last--;//删除无用平面     if(last-first<=1)return 0;//空集     p[last]=GetIntersection(q[last],q[first]);//计算首尾两个半平面的交点     int m=0;//从deque复制到输出     for(int i=first;i<=last;i++)poly[m++]=p[i];     return m; } int main() {     int n;     while(cin>>n&&n!=0)     {         Point p[210],poly[210];         Line L[210];         Point V[210],V2[210];         for(int i=0;i<n;i++)             cin>>p[i].x>>p[i].y;         for(int i=0;i<n;i++)         {             V[i]=p[(i+1)%n]-p[i];             V2[i]=Normal(V[i]);         }         double left=0,right=20000;         while(right-left>1e-6)         {             double mid=(left+right)/2;             for(int i=0;i<n;i++)                 L[i]=Line(p[i]+V2[i]*mid,V[i]);             int m=HalfplaneIntersection(L,n,poly);             if(!m)right=mid;             else left=mid;         }         cout<<setiosflags(ios::fixed)<<setprecision(6);         cout<<left<<endl;     }     return 0; }





转载于:https://www.cnblogs.com/MisdomTianYa/p/6581756.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值