Enumerates any two points as straight lines.
See if you can cross the entire pipe.
Determine intersection using cross product.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <cstdlib> #include <cmath> #include <map> #include <sstream> #include <queue> #include <vector> #define MAXN 100005 #define MAXM 211111 #define EPS 1e-8 #define INF 50000001 using
namespace Std;
inline int dblcmp (double d) {if (Fabs (d) < EPS) return 0; Return d > EPS?
1:-1;
} struct Point {double x, y;
Point () {} point (double _x, double _y): X (_x), Y (_y) {} void input () {scanf ("%lf%lf", &x, &y);
} bool operator = = (Point a) const {return dblcmp (a.x-x) = = 0 && dblcmp (a.y-y) = = 0;
} point Sub (point P) {return point (x-p.x, y-p.y);
} double dot (point P) {return x * p.x + y * P.Y;
} double det (point p) {return x * p.y-y * p.x;
} double distance (point p) { Return Hypot (x-p.x, Y-P.Y);
}}a[25], b[25];
struct Line {point A, B;
Line () {} line (point _a, point _b) {a = _a; b = _b;}
void input () {a.input ();
B.input ();
} int segcrossseg (line v) {int d1 = dblcmp (B.sub (a). Det (V.a.sub (a)));
int d2 = DBLCMP (B.sub (a). Det (V.b.sub (a)));
int d3 = DBLCMP (V.b.sub (V.A). Det (A.sub (V.A)));
int d4 = dblcmp (V.b.sub (V.A). Det (B.sub (V.A)));
if (d1 ^ d2) = =-2 && (d3 ^ d4) = =-2) return 2;
return (D1 = = 0 && dblcmp (v.a.sub (a). dot (v.a.sub (b))) <= 0| |
D2 = = 0 && dblcmp (v.b.sub (a). dot (v.b.sub (b))) <= 0| |
D3 = = 0 && dblcmp (a.sub (V.A). Dot (A.sub (V.B))) <= 0| |
D4 = = 0 && dblcmp (b.sub (V.A). Dot (B.sub (V.B))) <= 0);
} int linecrossseg (line v)//v is seg {int d1 = dblcmp (B.sub (a). Det (V.a.sub (a)));
int d2 = DBLCMP (B.sub (a). Det (V.b.sub (a))); if ((d1 ^ d2) = =-2) return 2;
return (D1 = = 0 | | d2 = = 0);
} point CrossPoint (line v) {Double A1 = V.b.sub (V.A). Det (A.sub (V.A));
Double A2 = V.b.sub (V.A). Det (B.sub (V.A));
Return Point ((a.x * a2-b.x * A1)/(A2-A1), (A.Y * A2-B.Y * A1)/(A2-A1));
}
};
int n;
Double ans;
int Gao (line x, int id) {int ind =-1;
int kind =-1; for (int i = 0; i < n-1; i + +) {if (dblcmp (X.b.sub (x.a). Det (a[i].sub (X.A))) < 0 | | dblcmp (X.B.SUB (X.A).
Det (a[i + 1].sub (X.A))) < 0) {ind = i;
kind = 1;
Break
} if (dblcmp (X.b.sub (x.a). Det (b[i].sub (x.a)) > 0 | | dblcmp (X.B.SUB (x.a). Det (b[i + 1].sub (X.A))) > 0)
{ind = i;
Kind = 2;
Break
}} if (Ind! =-1 && ind < ID) return 0;
if (Ind = =-1) return 1;
Line y;
if (kind = = 1) y = line (A[ind], A[ind + 1]); Else y = line (b[IND], B[ind + 1]);
Point C = x.crosspoint (y);
ans = max (ans, c.x);
return 0;
} int main () {while (scanf ("%d", &n)! = EOF && N) {for (int i = 0; i < n; i++) {
A[i].input ();
b[i].x = a[i].x;
B[i].y = a[i].y-1;
} ans = a[0].x;
int flag = 0; for (int i = 0, i < n; i++) for (int j = i + 1; j < N; j + +) {line x = line (a[
I], b[j]);
Flag |= Gao (x, J);
x = line (B[i], a[j]);
Flag |= Gao (x, J);
if (flag) break;
} if (flag) puts ("Through all the pipe.");
else printf ("%.2f\n", ans);
} return 0; }