Violent enumeration angle .....
Stealing a CakeTime
limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2495 Accepted Submission (s): 681
Problem Descriptionthere is a big round cake on the ground. A small ant plans to steal a small piece of cake. He starts from a certain point, reaches the cake, and then carry the piece back home. He does not want to being detected, so he was going to design a shortest path to achieve his goal.
The big cake can considered as a circle on a 2D plane. The ant ' s home can be considered as a rectangle. The ant can walk through the cake. Please find the shortest path for the poor ant.
Inputthe input consists of several test cases.
The first line of all test case contains X, Y, representing the coordinate of the starting point. The second line contains x, Y, R. The center of the cake is point (x, y) and the radius of the cake is R. The third line contains X1,y1,x2,y2, representing the coordinates of both opposite vertices of the rectangle---the ant ' s Home.
All numbers in the input is real numbers range from-10000 to 10000. It is guaranteed that the cake and the ant's home don ' t overlap or contact, and the ant's starting point also are not insid E The cake or his home, and doesn ' t contact with the cake or his home.
If the ant touches any part of home, then he's at home.
Input ends with a line of 0 0. There may is a blank line between the test cases.
Outputfor each test case, print the shortest distance to achieve his goal. Round the result to 2 digits after decimal point.
Sample Input
1 1-1 1 10-1 1 00 2-1 1 10-1 1 00 0
Sample Output
1.752.00
Source2012 Asia Hangzhou Regional Contest
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath>using namespace Std;const double eps=1e-6;int dcmp (double x) {if (Fabs (x) <eps) return 0; Return (x<0)? -1:1;} struct point{double x, y; Point (double _x=0,double _y=0) {x=_x; y=_y;}}; Point operator+ (Point A,point B) {return point (A.X+B.X,A.Y+B.Y);} Point operator-(Point A,point B) {return point (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 Cross (point A,point B) {return a.x*b.y-a.y*b.x;} Double Dot (Point A,point B) {return a.x*b.x+a.y*b.y;} Double Length (Point A) {return sqrt (Dot (a,a));} Double Distancetosegment (Point p,point a,point B) {if (a==b) return Length (P-A); Point 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); Const double Pi=acos ( -1.0); struct Circle{point C; Double R; Circle (Point _c=0,double _r=0): C (_c), R (_r) {} point point (Double a) {return point (C.x+cos (a) *r,c.y+sin (a) *r); }};d ouble a,b,c,d; Point P1; Point L[4]; Circle C;int Main () {while (scanf ("%lf%lf", &a,&b)!=eof) {if (dcmp (a) ==0&&dcmp (b) ==0) break; P1=point (A, b); scanf ("%lf%lf%lf", &a,&b,&c); C=circle (Point (A, B), c); scanf ("%lf%lf%lf%lf", &a,&b,&c,&d); if (a>c) swap (A,C); if (b>d) swap (B,D); L[0]=point (A, b); L[1]=point (A,D); L[2]=point (C,D); L[3]=point (C,B); Double delta=2.0*pi*0.0001; Double Ans=1e30; for (int i=0;i<10000;i++) {double du=delta*i; Point P = Point (C.c.x+cos (du) *c.r,c.c.y+sin (du) *C.R); Double part1=distancetosegment (p,l[3],l[0]); for (int i=0;i<3;i++) {Double temp = distancetosegment (p,l[i],l[i+1]); Part1=min (part1,temp);} Double Part2=length (P-P1); Ans=min (anS,PART1+PART2); } printf ("%.2lf\n", ans); } return 0;}
Copyright: Code of the Ape Ape from the AC road Http://blog.csdn.net/ck_boss
Hdoj 4454 stealing a Cake computational geometry