题目链接 题目大意:给你一个蚂蚁的x,y坐标,再给你一个圆蛋糕的圆心坐标与半径,再给你一个矩形的两个对角线的坐标,矩形表示蚂蚁的家。问你这只蚂蚁跑到蛋糕那里再回到家的最短路线是多少。抽象出来就是在圆上找一个点,使得这一点到蚂蚁初始坐标的距离与这一点到蚂蚁家的距离之和最短。 思路:通过角度枚举圆上的点,枚举2000个点,计算该点到起始点距离,再加上该点到矩形的最短距离,点到矩形的最短距离可通过点到矩形四条线段的最短距离来计算,具体代码如下: //Stealing a Cake.cpp #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define LL long long #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define EXIT exit(0); #define DEBUG puts("Here is a BUG"); #define CLEAR(name, init) memset(name, init, sizeof(name)) const double eps = 1e-8; const int MAXN = (int)1e9 + 5; const double PI = acos(-1.0); using namespace std; struct Point { double x, y; Point(double x=0, double y=0):x(x),y(y) { } }ant; typedef Point Vector; struct Circle { Point center; double radius; }cake; struct Rectangle { Point lu, ld, ru, rd; Rectangle() {} Rectangle(Point t1, Point t2) { lu.x = min(t1.x, t2.x); lu.y = max(t1.y, t2.y); ld.x = min(t1.x, t2.x); ld.y = min(t1.y, t2.y); ru.x = max(t1.x, t2.x); ru.y = max(t1.y, t2.y); rd.x = max(t1.x, t2.x); rd.y = min(t1.y, t2.y); } }home; Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); } Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); } Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (const Vector& A, double p) { return Vector(A.x/p, A.y/p); } int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } 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 dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; } double Length(const Vector& A) { return sqrt(Dot(A, A)); } double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; } double DistanceToSegment(const Point& P, const Point& A, const Point& B) { if(A == B) return Length(P-A); Vector v1 = B - A, v2 = P - A, v3 = P - B; if(dcmp(Dot(v1, v2)) < 0) return Length(v2); else if(dcmp(Dot(v1, v3)) > 0) return Length(v3); else return fabs(Cross(v1, v2)) / Length(v1); } double solve(double angle) { Point tmp; tmp.x = cake.center.x + cake.radius*cos(angle); tmp.y = cake.center.y + cake.radius*sin(angle); double res = sqrt((tmp.x-ant.x)*(tmp.x-ant.x) + (tmp.y-ant.y)*(tmp.y-ant.y)); double dis = DistanceToSegment(tmp, home.lu, home.ld); dis = min(dis, DistanceToSegment(tmp, home.ld, home.rd)); dis = min(dis, DistanceToSegment(tmp, home.rd, home.ru)); dis = min(dis, DistanceToSegment(tmp, home.ru, home.lu)); return res + dis; } int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin); #endif while(cin >> ant.x >> ant.y) { if (ant.x == 0 && ant.y == 0) break; cin >> cake.center.x >> cake.center.y >> cake.radius; Point t1, t2; cin >> t1.x >> t1.y >> t2.x >> t2.y; home = Rectangle(t1, t2); double res = INT_MAX; for(int i = 0; i < 2000; i++) { double tmp = solve(i*0.001*PI); res = min(tmp, res); } printf("%.2lf\n", res); } return 0; }