题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4454
思路:枚举角度,确定圆上点的位置,取最小值。
点到矩形最短距离:若点在矩形边所表示的范围内,则到矩形最短距离为x或y坐标到矩形边的距离。否则为点到矩形顶点的距离。
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> #define debu using namespace std; const double pi=acos(-1.0); struct Point { double x,y,r; }; Point C,P,st; double x1,y1,x2,y2; double miny,maxy,minx,maxx; double dist(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double Rdist(Point a) { double x=0.0,y=0.0; if(a.x<minx) x=minx-a.x; else if(a.x>maxx) x=a.x-maxx; if(a.y<miny) y=miny-a.y; else if(a.y>maxy) y=a.y-maxy; return sqrt(x*x+y*y); } int main() { #ifdef debug freopen("in.in","r",stdin); #endif // debug while(scanf("%lf%lf",&st.x,&st.y)!=EOF) { if(st.x==0&&st.y==0) break; double ans=1e10; scanf("%lf%lf%lf",&C.x,&C.y,&C.r); scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); minx=min(x1,x2),maxx=max(x1,x2); miny=min(y1,y2),maxy=max(y1,y2); for(double rad=0; rad<=360; rad+=0.005) { Point P; P.x=C.x+C.r*cos(rad/180*pi); P.y=C.y+C.r*sin(rad/180*pi); ans=min(ans,dist(P,st)+Rdist(P)); } printf("%.2f\n",ans); } return 0; }
时间: 2024-10-10 22:35:05