Question:
Starting from any point on the periphery of a square, how many line segments can be used to reach a specified point at least? (You can only pass through the midpoint of a line segment at a time)
Question: Train of Thought 1: enumerate the peripheral start point, connect the point to the specified point, and the answer is the minimum number of intersections. Train of Thought 2: first find the midpoint of all line segments. Starting from BFs at a specified point, the minimum number of steps that walk out of the square is the result. During the search process, adjacent points (u, v) meet the condition that the connections of U and V do not conform to any other line segments.
# Include <cmath> # include <cstdio> # include <algorithm> Using STD: sort; # define EPS 1e-8 # define zero (x)> 0? (X):-(x) <EPS) struct point {Double X, Y ;}; struct line {point A, B ;}; // calculate cross product (P1-P0) X (P2-P0) Double xmult (point P1, point P2, point P0) {return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p2.x-Snapshot X) * (p1.y-p0.y);} double xmult (double X1, double Y1, double X2, double Y2, double x0, double y0) {return (x1-x0) * (y2-y0) -(x2-x0) * (y1-y0);} // calculate the dot product (P1-P0 ). (P2-P0) Double dmult (point P1, point P2, point P0) {return (p1.x-rows X) * (P 2. x-p0.x) + (p1.y-week Y) * (p2.y-week Y);} double dmult (double X1, double Y1, double X2, double Y2, double x0, double y0) {return (x1-x0) * (x2-x0) + (y1-y0) * (y2-y0);} // two-point distance from double distance (point P1, point P2) {return SQRT (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y);} double distance (double X1, double Y1, double X2, double Y2) {return SQRT (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2);} // returns a three-point collinearity int dots_inline (point P1, point P2, Poin T P3) {return zero (xmult (P1, P2, P3);} int dots_inline (double X1, double Y1, double X2, double Y2, double X3, double Y3) {return zero (xmult (x1, Y1, X2, Y2, X3, Y3);} // determine whether the vertex is on the online segment, including the endpoint int dot_online_in (point P, line L) {return zero (xmult (P, L. a, L. b) & (L. a. x-p.x) * (L. b. x-p.x) <EPS & (L. a. y-p.y) * (L. b. y-p.y) <EPS;} int dot_online_in (point P, point L1, point L2) {return zero (xmult (p, L1, L2) & (l1.x-P. x) * (l2.x-P. x) <EPS & (l1.y-P. Y) * (L2.y-P. y) <EPS;} int dot_online_in (Double X, Double Y, double X1, double Y1, double X2, double Y2) {return zero (xmult (X, Y, X1, y1, X2, Y2) & (x1-x) * (x2-x) <EPS & (y1-y) * (y2-y) <EPS ;}// determine whether the spot is on the online segment, the endpoint int dot_online_ex (point P, line L) {return dot_online_in (P, L )&&(! Zero (P. x-l.a.x) |! Zero (P. y-l.a.y ))&&(! Zero (P. x-l. B .x) |! Zero (P. y-l. B .y);} int dot_online_ex (point P, point L1, point L2) {return dot_online_in (p, L1, L2 )&&(! Zero (P. x-l1.x) |! Zero (P. y-l1.y ))&&(! Zero (P. x-l2.x) |! Zero (P. y-l2.y);} int dot_online_ex (Double X, Double Y, double X1, double Y1, double X2, double Y2) {return dot_online_in (X, Y, X1, Y1, X2, y2 )&&(! Zero (x-x1) |! Zero (y-y1 ))&&(! Zero (x-x2) |! Zero (y-y2);} // returns 0int same_side (point P1, point P2, line L) {return xmult (L. a, P1, L. b) * xmult (L. a, P2, L. b)> EPS;} int same_side (point P1, point P2, point L1, point L2) {return xmult (L1, P1, L2) * xmult (L1, P2, l2)> EPS;} // determine the different sides of the two online segments. The 0int opposite_side (point P1, point P2, line L) {return xmult (L. a, P1, L. b) * xmult (L. a, P2, L. b) <-EPS;} int opposite_side (point P1, point P2, point L1, point L2) {return xmult (L1, P1, L2) * xmult (L1, P2, L2) <-EPS;} // judge two straight-line parallel int parallel (line U, line V) {return zero (U. a. x-u. B .x) * (v. a. y-v. B .y)-(V. a. x-v. B .x) * (U. a. y-u. B .y);} int parallel (point u1, point U2, point V1, point V2) {return zero (u1.x-u2.x) * (v1.y-v2.y) -(v1.x-v2.x) * (u1.y-u2.y);} // judge two straight lines vertical int perpendicular (line U, line V) {return zero (U. a. x-u. B .x) * (v. a. x-v. B .x) + (U. a. y-u. B .y) * (v. a. y-v. B .y);} int perpendicular (point u1, point U2, point V1, Point V2) {return zero (u1.x-u2.x) * (v1.x-v2.x) + (u1.y-u2.y) * (v1.y-v2.y ));} // determine the intersection of two line segments, including the endpoint and partial coincidence int intersect_in (line U, line V) {If (! Dots_inline (U. A, U. B, V. a) |! Dots_inline (U. A, U. B, V. B) return! Same_side (U. A, U. B, v )&&! Same_side (v. a, V. b, U); Return dot_online_in (U. a, v) | dot_online_in (U. b, v) | dot_online_in (v. a, u) | dot_online_in (v. b, U);} int intersect_in (point u1, point U2, point V1, point V2) {If (! Dots_inline (U1, U2, V1) |! Dots_inline (U1, U2, V2) return! Same_side (U1, U2, V1, V2 )&&! Same_side (V1, V2, u1, U2); Return dot_online_in (U1, V1, V2) | dot_online_in (U2, V1, V2) | dot_online_in (V1, u1, U2) | dot_online_in (V2, u1, U2);} // identifies the intersection of two line segments, excluding the endpoint and partial coincidence int intersect_ex (line U, line V) {return opposite_side (U. a, U. b, v) & opposite_side (v. a, V. b, U);} int intersect_ex (point u1, point U2, point V1, point V2) {return opposite_side (U1, U2, V1, V2) & opposite_side (V1, v2, u1, U2) ;}// calculate the intersection of two straight lines. Check whether the line is parallel in advance! // Determine the intersection of line segments separately (parallel or not at the same time !) Point intersection (line U, line V) {point ret = u. a; Double T = (U. a. x-v.a.x) * (v. a. y-v. B .y)-(U. a. y-v.a.y) * (v. a. x-v. B .x)/(U. a. x-u. B .x) * (v. a. y-v. B .y)-(U. a. y-u. B .y) * (v. a. x-v. B .x); ret. X + = (U. b. x-u.a.x) * t; ret. Y + = (U. b. y-u.a.y) * t; return ret;} point intersection (point u1, point U2, point V1, point V2) {point ret = U1; Double T = (u1.x-v1.x) * (v1.y-v2.y)-(u1.y-v1.y) * (v1.x-v2.x)/(u1.x-u2.x) * (v1.y-v2.y)-(u1.y-u2.y) * (v1.x -V2.x); ret. X + = (u2.x-u1.x) * t; ret. Y + = (u2.y-u1.y) * t; return ret;} // point ptoline (point P, line L) {point T = P; T. X + = L. a. y-l. B .y, T. Y + = L. b. x-l.a.x; return intersection (P, T, L. a, L. b);} Point ptoline (point P, point L1, point L2) {point T = P; T. X + = l1.y-l2.y, T. Y + = l2.x-l1.x; return intersection (P, T, L1, L2);} // distance from a point to a straight line to double disptoline (point P, line L) {return FABS (xmult (P, L. a, L. b)/distance (L. a, L. b);} double disptoli Ne (point P, point L1, point L2) {return FABS (xmult (p, L1, L2)/distance (L1, L2);} double disptoline (Double X, double Y, double X1, double Y1, double X2, double Y2) {return FABS (xmult (X, Y, X1, Y1, X2, Y2)/distance (x1, y1, X2, Y2);} // point ptoseg (point P, line L) {point T = P; T. X + = L. a. y-l. B .y, T. Y + = L. b. x-l.a.x; If (xmult (L. a, T, P) * xmult (L. b, T, P)> EPS) return distance (P, L. a) <distance (P, L. B )? L. a: l. b; return intersection (P, T, L. a, L. b);} Point ptoseg (point P, point L1, point L2) {point T = P; T. X + = l1.y-l2.y, T. Y + = l2.x-l1.x; If (xmult (L1, T, P) * xmult (L2, T, P)> EPS) return distance (p, L1) <distance (p, L2 )? L1: L2; return intersection (P, T, L1, L2);} // distance from a point to a line segment to double disptoseg (point P, line L) {point T = P; T. X + = L. a. y-l. B .y, T. Y + = L. b. x-l.a.x; If (xmult (L. a, T, P) * xmult (L. b, T, P)> EPS) return distance (P, L. a) <distance (P, L. B )? Distance (P, L. a): distance (P, L. b); Return FABS (xmult (P, L. a, L. b)/distance (L. a, L. b);} double disptoseg (point P, point L1, point L2) {point T = P; T. X + = l1.y-l2.y, T. Y + = l2.x-l1.x; If (xmult (L1, T, P) * xmult (L2, T, P)> EPS) return distance (p, L1) <distance (p, L2 )? Distance (p, L1): distance (p, L2); Return FABS (xmult (p, L1, L2)/distance (L1, L2 );} // vector V uses P as the vertex to rotate the angle counterclockwise and enlarge the scale point rotate (point V, point P, double angle, double scale) {point ret = P; V. x-= P. x, V. y-= P. y; p. X = scale * Cos (angle); p. y = scale * sin (angle); ret. X + = v. x * P. x-v.y * P. y; ret. Y + = v. x * P. Y + v. y * P. x; return ret;} Point getmid (point a, point B) {point TMP; TMP. X = (. X + B. x)/2; TMP. y = (. Y + B. y)/2; return TMP;} bool CMP (Const point & A, const point & B) {point O; O. X = O. y = 0; int TMP = xmult (a, B, O); If (zero (TMP) return distance (A, O) <distance (B, O ); return TMP> 0;} point P [1000]; line L [1000]; int main () {int N; point target, mid; scanf ("% d ", & N); int I, j, c = 0; for (I = 0; I <n; I ++) {scanf ("% lf", & L [I]. a. x, & L [I]. a. y, & L [I]. b. x, & L [I]. b. y); P [C]. X = L [I]. a. x; P [C ++]. y = L [I]. a. y; P [C]. X = L [I]. b. x; P [C ++]. Y = L [I]. b. y;} scanf ("% lf", & target. x, & target. y); P [C]. X = 0; P [C ++]. y = 0; P [C]. X = 0; P [C ++]. y = 100; P [C]. * = 100; P [C ++]. y = 0; P [C]. * = 100; P [C]. y = 100; sort (p, p + C, CMP); int ans = 99999999, step; for (I = 1; I <= C; I ++) {mid = getmid (P [I], p [I-1]); For (j = step = 0; j <n; j ++) if (intersect_ex (MID, target, L [J]. a, L [J]. b) Step ++; ans = Step <ans? Step: ans;} printf ("number of doors = % d \ n", ANS + 1); Return 0 ;}