Simple geometry (segment intersection) POJ 1066 Treasure Hunt

Source: Internet
Author: User

Topic Portal

Test instructions: From any point on the four sides, there are a number of obstacles door, ask at least to blow down a few doors to reach the end

Analysis: Enumerates the entry points, that is, the two endpoints of the segment, and then selects the 1 with the least number of points intersecting the other segments as the answer. When I was sentenced to n = = 0.

/************************************************* author:running_time* Created time:2015/10/26 Monday 16:30:26* Fi Le Name:P oj_1066.cpp ************************************************/#include <cstdio> #include < algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath># Include <string> #include <vector> #include <queue> #include <deque> #include <stack># Include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 33;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const Double EPS = 1e-10;const D    Ouble PI = ACOs ( -1.0), int dcmp (double x) {//three state function, reduced accuracy problem if (fabs (x) < EPS) return 0; else return x < 0? -1:1;}    struct Point {//points defined by double x, y; Point (Double x=0, double y=0): X (x), Y (y) {} point operator + (const point &r) const {//Vector addition return point (x    + r.x, y + r.y);    } Point operator-(const point &r) const {//Vector subtraction return point (x-r.x, Y-R.Y);    } Point operator * (double p) {///vector multiplied by a scalar return point (x * p, Y * p);    } point operator/(double p) {///vector divided by scalar return point (x/p, y/p); } BOOL operator < (const point &r) const {//DOT coordinate sort return x < R.x | |    (x = = r.x && y < r.y); } bool operator = = (Const point &r) const {//judgment same dot return dcmp (x-r.x) = = 0 && dcmp (y-r    . y) = = 0;       }};typedef Point Vector;    Vector definition point read_point (void) {//points read in double x, y;    scanf ("%lf%lf", &x, &y); Return point (x, y);} Double dot (vector A, vector B) {//vector dot product return a.x * b.x + a.y * B.Y;} Double Cross (vector A, vector B) {//vector fork product return a.x * B.Y-A.Y * b.x;} Double Polar_angle (vector A) {//Vector polar return atan2 (A.Y, a.x);} Double length (vector a) {//vector length, dot product return sqrt (dot (A, a));} Double angle (vector a, vector b) {//Vector corner, counterclockwise, dot product return ACOs (dot (A, b)/Length (a)/length (b));} Vector Rotate (vector A, double rad) {//vector rotation, counter-clockwise return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad ) + a.y * cos (RAD));}    Vector Nomal (vector a) {//vector double len = length (a) of the unit method vectors; Return Vector (-a.y/len, A.x/len);}    Point Line_line_inter (point P, Vector V, point q, Vector W) {//two line intersection, parametric equation Vector U = p-q;    Double T = Cross (w, U)/Cross (V, W); return p + V * t;}    Double Point_to_line the distance from {//points to a straight line, two-point Vector V1 = b-a, V2 = p-a; Return Fabs (Cross (V1, V2))/Length (V1);}    Double point_to_seg the distance from {//points to a line segment, two-point 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);}    Point Point_line_proj (point P, points a, dot b) {//dots are projected on a line, two points Vector V = b-a; Return a + v * (dot (v, p-a)/dot (V, v));}  BOOL Can_inter (Points A1, point A2, Spot b1, dot b2) {//Judgment segment intersection, two-bit double C1 = Cross (A2-A1, b1-a1), C2 =    Cross (A2-A1, b2-a1), C3 = Cross (B2-B1, a1-b1), C4 = Cross (b2-b1, A2-B1); Return DCMP (C1) * DCMP (C2) < 0 && dcmp (C3) * DCMP (C4) < 0;} BOOL On_seg (point P, bit A1, points A2) {//Judgment dot on segment, two-point return dcmp (Cross (a1-p, a2-p)) = = 0 && DCMP (dot (a1-p, a2-p)) < 0;} Double Area_triangle (Point A, point B, point C) {//triangular area, fork product return Fabs (Cross (b-a, c-a))/2.0;}    Double Area_poly (point *p, int n) {//Polygon area, cross product double ret = 0; for (int i=1; i<n-1; ++i)  {ret + = Fabs (Cross (P[i]-p[0], p[i+1]-p[0])); } return RET/2;}    /* Point set Convex package */vector<point> convex_hull (vector<point> &p) {sort (P.begin (), P.end ());    int n = p.size (), k = 0;    vector<point> ret (n * 2);         for (int i=0; i<n; ++i) {when (K > 1 && Cross (ret[k-1]-ret[k-2], P[i]-ret[k-1]) <= 0) k--;    ret[k++] = P[i]; } for (int i=n-2, t=k; i>=0;-I.) {while (K > t && Cross (Ret[k-1]-ret[k-2], P[i]-ret[k-1])        <= 0) k--;    ret[k++] = P[i];    } ret.resize (k-1);   return ret;}    struct Circle {point C;    Double R; Circle () {} circle (point C, Double R): C (c), R (r) {}-point-point (double a) {return-point (c.x + cos (    A) * R, C.y + sin (a) * R);    }};struct Line {point P;    Vector v;    Double R;    Line () {} line (const point &p, const Vector &v): P (P), V (v) {r = Polar_angle (v); } Point Point (DouBle a) {return P + v * A; }}; Point S[n], e[n];  Point P;int Cnt1[n], cnt2[n];int main (void) {int N;    scanf ("%d", &n);        for (int i=1; i<=n; ++i) {S[i] = Read_point ();    E[i] = Read_point ();    } p = Read_point ();   if (!n) {printf ("number of doors =%d\n", 1);    return 0;    } int ans = INF;            for (int i=1, i<=n; ++i) {for (int j=1; j<=n; ++j) {if (i = = j) Continue;            if (Can_inter (p, S[i], s[j], e[j])) {cnt1[i]++;            } if (Can_inter (p, E[i], s[j], e[j])) {cnt2[i]++;    }} ans = min (ans, min (cnt1[i], cnt2[i]));   } printf ("Number of doors =%d\n", ans + 1);    cout << "time Elapsed:" << 1.0 * Clock ()/clocks_per_sec << "s.\n"; return 0;}

  

Simple geometry (segment intersection) POJ 1066 Treasure Hunt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.