题意:

      求点到圆,然后在到矩形的最短路.

思路:

      把圆切成两半,然后对于每一半这个答案都是凸性的,最后输出两半中小的那个就行了,其中有一点,就是求点到矩形的距离,点到矩形的距离就是点到矩形四条边距离中最小的哪一个,求的方法有很多,一开始想都没想直接敲了个三分(这样就是两重三分了)跑了890ms AC的,后来看了看人家的都是直接用模板过的,我也找了个模板,结果31ms AC,哎,没事真的多暂歇模板,只有好处没坏处是了..


#include<stdio.h>
#include<math.h>
#include<algorithm> #define eps 1e-3
double PI=acos(-1.0); using namespace std; typedef struct
{
double
x ,y;
}
NODE; typedef struct
{

NODE A ,B;
}
EDGE; NODE node[10];
EDGE edge[10];
NODE S ,O;
double
diss1[10] ,diss2[10];
double
R; bool camp(NODE a ,NODE b)
{
return
a.x < b.x || a.x == b.x && a.y < b.y;
} double
dis(NODE a ,NODE b)
{
double
tmp = pow(a.x - b.x ,2.0) + pow(a.y - b.y ,2.0);
return
sqrt(tmp);
} double
minn(double x ,double y)
{
return
x < y ? x : y;
} double
abss(double x)
{
return
x > 0 ? x : -x;
}
NODE getnode(double du)
{

NODE ans;
ans.x = O.x + R *cos(du);
ans.y = O.y + R * sin(du);
return
ans;
}
//**********************************
struct Point
{
double
x,y;
Point(double xx=0,double yy=0):x(xx),y(yy){}
Point operator -(const Point p1)
{
return
Point(x-p1.x,y-p1.y);
}
double operator ^(const
Point p1)
{
return
x*p1.x+y*p1.y;
}
};
double
cross(Point a,Point b)
{
return
a.x*b.y-a.y*b.x;
}
inline int
sign(double d)
{
if(
d>eps)return 1;
else if(
d<-eps)return -1;
else return
0;
}
double
dis(Point A ,Point B)
{
return
sqrt(pow(A.x - B.x ,2.0) + pow(A.y - B.y ,2.0));
}
double
ptoline(Point tp,Point st,Point ed)//求点到线段的距离
{
double
t1=dis(tp,st);
double
t2=dis(tp,ed);
double
ans=min(t1,t2);
if(
sign((ed-st)^(tp-st))>=0 && sign((st-ed)^(tp-ed))>=0)//锐角
{
double
h=fabs(cross(st-tp,ed-tp))/dis(st,ed);
ans=min(ans,h);
}
return
ans;
}

//************************
double search3_2(double L ,double R)
{
double
low ,up ,mid ,mmid;
NODE now;
low = L ,up = R;
while(
1)
{

mid = (low + up) / 2;
now = getnode(mid);
Point A ,B ,C;
A.x = now.x ,A.y = now.y;
for(int
i = 1 ;i <= 4 ;i ++)
{

B.x = edge[i].B.x ,B.y = edge[i].B.y;
C.x = edge[i].A.x ,C.y = edge[i].A.y;
diss1[i] = ptoline(A ,B ,C) + dis(S ,now); }
sort(diss1 + 1 ,diss1 + 4 + 1);
mmid = (mid + up) / 2;
now = getnode(mmid); A.x = now.x ,A.y = now.y;
for(int
i = 1 ;i <= 4 ;i ++)
{

B.x = edge[i].B.x ,B.y = edge[i].B.y;
C.x = edge[i].A.x ,C.y = edge[i].A.y;
diss2[i] = ptoline(A ,B ,C) + dis(S ,now);
}

sort(diss2 + 1 ,diss2 + 4 + 1);
if(
diss1[1] > diss2[1]) low = mid;
else
up = mmid;
if(
abss(low - up) < eps) break;
}
return
diss1[1];
} int main ()
{
while(~
scanf("%lf %lf" ,&S.x ,&S.y))
{
if(
S.x == 0 && S.y == 0) break;
scanf("%lf %lf %lf" ,&O.x ,&O.y ,&R);
scanf("%lf %lf %lf %lf" ,&node[1].x ,&node[1].y ,&node[2].x ,&node[2].y);
node[3].x = node[1].x ,node[3].y = node[2].y;
node[4].x = node[2].x ,node[4].y = node[1].y;
sort(node + 1 ,node + 4 + 1 ,camp);
edge[1].A.x = node[1].x ,edge[1].A.y = node[1].y;
edge[1].B.x = node[2].x ,edge[1].B.y = node[2].y;
edge[2].A.x = node[2].x ,edge[2].A.y = node[2].y;
edge[2].B.x = node[4].x ,edge[2].B.y = node[4].y;
edge[3].A.x = node[4].x ,edge[3].A.y = node[4].y;
edge[3].B.x = node[3].x ,edge[3].B.y = node[3].y;
edge[4].A.x = node[3].x ,edge[4].A.y = node[3].y;
edge[4].B.x = node[1].x ,edge[4].B.y = node[1].y;
printf("%.2lf\n" ,minn(search3_2(0 ,PI) ,search3_2(PI ,2*PI)));
}
return
0;
}

最新文章

  1. BPM配置故事之案例9-根据表单数据调整审批线路2
  2. MMORPG大型游戏设计与开发(客户端架构 part10 of vegine)
  3. MyBatis_Generator的使用(实践)
  4. 困难的串(dfs)
  5. 【Android测试】【随笔】Bugtags初体验
  6. SOA之(2)——SOA架构基础概念与设计框架
  7. java 反射的踩的一个坑
  8. HTTP请求返回的NSData无法转换为NSString
  9. Android 自定义View之BounceProgressBar
  10. Mysql连接问题:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException
  11. C++入门篇十三
  12. jackson json转bean忽略没有的字段 not marked as ignorable
  13. Java Scanner nextLine方法跳过
  14. 8 种常被忽视的 SQL 错误用法
  15. [LeetCode] 577. Employee Bonus_Easy tag: SQL
  16. day10-连接mysql虚拟机报错
  17. PHP开发工程师-技能树
  18. 【镜像地址】Maven地址列表
  19. CentOS下 Uptime 命令
  20. 安装GraphicsMagick

热门文章

  1. HDOJ-1358(字符串压缩+KMP)
  2. 人脸识别分析小Demo
  3. threejs 基础概要
  4. 分布式session实现方式
  5. jwt以及如何使用jwt实现登录
  6. css整理之-----------技巧、黑魔法
  7. Go语言学习笔记——在本地建立一个官网查看
  8. 仿String()构造器函数 【总结】
  9. 对用pyinstaller打包的exe程序进行反编译,获得源码
  10. 攻防世界 reverse easy_Maze