Question: give a point, a circle, a rectangle, find a line, starting from a point, to a circle, and then to the shortest distance of the rectangle.
Solution: because the answer requires two decimal places to be output, and the accuracy requirement is not very high, you can try to get the point on the circle from the violent angle, and then find a distance, the distance between a vertex and a rectangle is the distance between the vertex and the four sides, then the minimum value is obtained, and then the total minimum value is obtained.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> # include <algorithm> # define Mod 1000000007 # define pi acos (- 1.0) # define eps 1e-8using namespace std; # define N 1000107 typedef struct Point {double x, y; Point (double x = 0, double y = 0): x (x ), y (y) {}point () {}} Vector; int sgn (double x) {if (x> eps) return 1; if (x <-eps) return-1; return 0;} Vector operator + (Vector A, Ve Ctor B) {return Vector (. x + B. x,. y + B. y);} Vector operator-(Point A, Point B) {return Vector (. x-B.x,. y-B.y);} bool operator = (const Point & a, const Point & B) {return (sgn (. x-b.x) = 0 & sgn (. y-b.y) = 0);} double Dot (Vector A, Vector B) {return. x * B. x +. y * B. y;} double Cross (Vector A, Vector B) {return. x * B. y-. y * B. x;} double Length (Vector A) {return sqrt (Dot (A, A);} double PtoSeg (P Oint P, Point A, Point B) {if (A = B) return Length (P-A); Vector V1 = B-A; Vector V2 = P-A; Vector V3 = P-B; if (sgn (Dot (V1, V2) <0) return Length (V2); else if (sgn (Dot (V1, V3)> 0) return Length (V3); else return fabs (Cross (V1, V2)/Length (V1) ;}int main () {double X, Y; double Cx, Cy, r; double x1, y1, x2, y2; while (scanf ("% lf", & X, & Y )! = EOF) {if (sgn (X) = 0 & sgn (Y) = 0) break; scanf ("% lf", & Cx, & Cy, & R); scanf ("% lf", & x1, & y1, & x2, & y2); Point A = Point (x1, y1); Point B = Point (x1, y2); Point C = Point (x2, y1); Point D = Point (x2, y2 ); double delta = 2.0 * pi * 0.0001; double Min = Mod; for (int Angle = 1; Angle <= 10000; Angle ++) {double ang = delta * Angle; double nx = Cx + R * cos (ang); double ny = Cy + R * sin (ang); double D1 = PtoSeg (Point (nx, ny),, b); double D2 = PtoSeg (Point (nx, ny), A, C); double D3 = PtoSeg (Point (nx, ny), B, D ); double D4 = PtoSeg (Point (nx, ny), C, D); double Dis = sqrt (nx-X) * (nx-X) + (ny-Y) * (ny-Y) + min (D1, D2), D3), D4); min = Min (Dis, min);} printf ("%. 2f \ n ", Min);} return 0 ;}View Code
HDU 4454 Stealing a Cake -- enumeration