題意:平面上有一些半徑爲R的圓,如今要在知足不與現有圓相交的條件下放入一個圓,求這個圓能放的位置的圓心到原點的最短距離。ios
解法:咱們將半徑擴大一倍,R = 2*R,那麼在每一個圓上或圓外的位置均可以放圓心了。ide
首先特判放到原點可不能夠,若是不能夠,再將全部圓的圓心與原點的直線與該圓相交的點放入隊列,再將全部圓兩兩相交的點放入隊列,而後處理整個隊列,一一判斷這些點行不行,能夠證實,最優勢必定在這些裏面。spa
若是有一個圓的圓心在(0,0)點,那麼要特判一下,由於此時圓心與原點連的直線長度爲0,對於這種狀況,咱們判一下(R,0)這個就好了。code
代碼:blog
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <vector> #define Mod 1000000007 #define eps 1e-7 using namespace std; struct Point{ double x,y; Point(double x=0, double y=0):x(x),y(y) {} void input() { scanf("%lf%lf",&x,&y); } }; typedef Point Vector; struct Circle{ Point c; double r; Circle(){} Circle(Point c,double r):c(c),r(r) {} Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); } void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); } }; int dcmp(double x) { if(x < -eps) return -1; if(x > eps) return 1; return 0; } 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) == 0 && dcmp(a.y-b.y) == 0; } 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; } double angle(Vector v) { return atan2(v.y, v.x); } bool InCircle(Point x, Circle c) { return dcmp(c.r - Length(c.c-x)) > 0; } //not in border int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol) //return 交點個數 { double d = Length(C1.c - C2.c); if(dcmp(d) == 0){ if(dcmp(C1.r - C2.r) == 0) return -1; //兩圓重合 return 0; } if(dcmp(C1.r + C2.r - d) < 0) return 0; if(dcmp(fabs(C1.r - C2.r) - d) > 0) return 0; double a = angle(C2.c - C1.c); //向量C1C2的極角 double da = acos((sqr(C1.r) + sqr(d) - sqr(C2.r)) / (2*C1.r*d)); //C1C2到C1P1的極角 Point p1 = C1.point(a-da), p2 = C1.point(a+da); sol.push_back(p1); if(p1 == p2) return 1; sol.push_back(p2); return 2; } double DISP(Point p) { return sqrt(p.x*p.x+p.y*p.y); } Circle C[106],sC[106]; int n; bool check(Point now) { for(int i=1;i<=n;i++) { if(InCircle(now,C[i])) return false; } return true; } int main() { int i,j; double R; while(scanf("%d%lf",&n,&R)!=EOF && n+R) { for(i=1;i<=n;i++) { scanf("%lf%lf",&C[i].c.x,&C[i].c.y), C[i].r = 2.0*R; sC[i] = C[i], sC[i].r = R; } vector<Point> sec; sec.clear(); for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) GetCircleCircleIntersection(C[i],C[j],sec); } double Mini = Mod; if(check(Point(0,0))) { printf("%.6f\n",0.0); continue; } for(i=1;i<=n;i++) { if(dcmp(DISP(C[i].c)) == 0) { if(check(Point(2*R,0))) Mini = min(Mini,2*R); continue; } sec.push_back(Point(C[i].c+C[i].c*(-2.0*R/DISP(C[i].c)))); sec.push_back(Point(C[i].c+C[i].c*(2.0*R/DISP(C[i].c)))); } for(i=0;i<sec.size();i++) if(check(sec[i])) Mini = min(Mini,DISP(sec[i])); printf("%.6f\n",Mini); } return 0; }