POJ 3525 Most Distant Point from the Sea [二分+半平面交]
来源:互联网 发布:越南经济发展现状知乎 编辑:程序博客网 时间:2024/06/07 00:22
题意:给一个凸多边形,求能容纳的最大圆的半径。
题解:由于是凸多边形,所以我们用半平面交向内压缩r的距离,直到不存在内核说明当前r是最大的圆半径,所以我们二分半径找到最大的符合要求的半径。
AC代码:
#include<iostream> #include <stdio.h> #include <math.h> #define eps 1e-8 using namespace std; const int MAXN=1505; int m; double r; int cCnt,curCnt;//此时cCnt为最终切割得到的多边形的顶点数、暂存顶点个数 struct point { double x,y; }; point points[MAXN],p[MAXN],q[MAXN];//读入的多边形的顶点(顺时针)、p为存放最终切割得到的多边形顶点的数组、暂存核的顶点 void getline(point x,point y,double &a,double &b,double &c){//两点x、y确定一条直线a、b、c为其系数 a = y.y - x.y; b = x.x - y.x; c = y.x * x.y - x.x * y.y; } void initial(){ for(int i = 1; i <= m; ++i)p[i] = points[i]; p[m+1] = p[1]; p[0] = p[m]; cCnt = m;//cCnt为最终切割得到的多边形的顶点数,将其初始化为多边形的顶点的个数 } point intersect(point x,point y,double a,double b,double c){//求x、y形成的直线与已知直线a、b、c、的交点 double u = fabs(a * x.x + b * x.y + c); double v = fabs(a * y.x + b * y.y + c); point pt; pt.x=(x.x * v + y.x * u) / (u + v); pt.y=(x.y * v + y.y * u) / (u + v); return pt; } void cut(double a,double b ,double c){ curCnt = 0; for(int i = 1; i <= cCnt; ++i){ if(a*p[i].x + b*p[i].y + c >= 0)q[++curCnt] = p[i];// c由于精度问题,可能会偏小,所以有些点本应在右侧而没在, //故应该接着判断 else { if(a*p[i-1].x + b*p[i-1].y + c > 0){//如果p[i-1]在直线的右侧的话, //则将p[i],p[i-1]形成的直线与已知直线的交点作为核的一个顶点(这样的话,由于精度的问题,核的面积可能会有所减少) q[++curCnt] = intersect(p[i],p[i-1],a,b,c); } if(a*p[i+1].x + b*p[i+1].y + c > 0){//原理同上 q[++curCnt] = intersect(p[i],p[i+1],a,b,c); } } } for(int i = 1; i <= curCnt; ++i)p[i] = q[i];//将q中暂存的核的顶点转移到p中 p[curCnt+1] = q[1];p[0] = p[curCnt]; cCnt = curCnt; } void solve(){ //注意:默认点是顺时针,如果题目不是顺时针,规整化方向 initial(); /*for(int i = 1; i <= m; ++i){ double a,b,c; getline(points[i],points[i+1],a,b,c); cut(a,b,c); } */ // 如果要向内推进r,用该部分代替上个函数 for(int i = 1; i <= m; ++i){ point ta, tb, tt; tt.x = points[i+1].y - points[i].y; tt.y = points[i].x - points[i+1].x; double k = r / sqrt(tt.x * tt.x + tt.y * tt.y); tt.x = tt.x * k; tt.y = tt.y * k; ta.x = points[i].x + tt.x; ta.y = points[i].y + tt.y; tb.x = points[i+1].x + tt.x; tb.y = points[i+1].y + tt.y; double a,b,c; getline(ta,tb,a,b,c); cut(a,b,c); } //多边形核的面积 /*double area = 0; for(int i = 1; i <= curCnt; ++i) area += p[i].x * p[i + 1].y - p[i + 1].x * p[i].y; area = fabs(area / 2.0); printf("%.2f\n",area); */} void GuiZhengHua(){ //规整化方向,逆时针变顺时针,顺时针变逆时针 for(int i = 1; i < (m+1)/2; i ++) swap(points[i], points[m-i]); } int main() {while(cin>>m) { if(m==0)break;int i; for(i=1;i<=m;i++)cin>>points[i].x>>points[i].y; GuiZhengHua();points[m+1]=points[1]; double L=0.0,R=10000.0; for(int i=0;i<100;i++){double mid=(L+R)/2.0;r=mid;solve();if(cCnt<1)R=mid;else L=mid;} printf("%.6f\n",L);} }
阅读全文
0 0
- POJ 3525 Most Distant Point from the Sea(半平面交+二分)
- Poj 3525 Most Distant Point from the Sea (二分+半平面求交)
- POJ 3525 Most Distant Point from the Sea 半平面交 +二分
- poj 3525 Most Distant Point from the Sea(半平面交+二分)
- POJ 3525 Most Distant Point from the Sea 二分+半平面交
- poj 3525 Most Distant Point from the Sea(半平面交+二分内推进)
- POJ 3525 Most Distant Point from the Sea 二分+半平面交
- poj 3525 Most Distant Point from the Sea 半平面交 + 二分
- poj 3525 Most Distant Point from the Sea,半平面交 + 二分
- 【二分+半平面交】 POJ 3525 Most Distant Point from the Sea
- POJ 3525 Most Distant Point from the Sea 二分+半平面交
- POJ 3525 Most Distant Point from the Sea 二分+内推+半平面交
- POJ 3525 Most Distant Point from the Sea 半平面交+二分
- poj 3525 Most Distant Point from the Sea(半平面交+二分)
- [凸包最大内接圆 二分 半平面交] POJ 3525 Most Distant Point from the Sea
- POJ 3525 Most Distant Point from the Sea (半平面交+二分求最大内切圆)
- poj 3525 Most Distant Point from the Sea(二分+半平面交)
- POJ 3525 Most Distant Point from the Sea (二分答案+半平面交)
- web.xml中load-on-startup的含义
- 采坑memset初始化vector的变量---vector iterators incompatible
- Java踩坑路
- Xcode-利用Configuration实现多个配置
- 5分钟教你玩转 sklearn 机器学习(上)
- POJ 3525 Most Distant Point from the Sea [二分+半平面交]
- 原生json获取数据
- JDK-9的安装记录
- 百度地图集成
- LeetCode算法题1-10
- bzoj4839: [Neerc2016]Abbreviation
- android service
- 希尔排序
- HUD 1114 Piggy-Bank 存钱罐 (完全背包 动态规划DP)