The Doors-poj 1556 (segment intersection)

Source: Internet
Author: User

Main topic: There is a room (upper left corner (0,10), lower right Corner (10,0)), and then the room has n wall, each wall has two doors, to find out from the initial point (0,5), to reach the end (10,5) the shortest distance.

 Analysis: It is obvious that according to the shortest line between two points, so the route must be a point between the connection, only need to judge the two points between the wall can be known.  The code is as follows:=========================================================================================================== ===========================
#include <math.h>#include<algorithm>#include<stdio.h>using namespacestd;Const intMAXN =1007;Const DoubleOO = 1e9+7;Const DoubleEPS = 1e-8;structpoint{Doublex, y, Len; Point (Doublex=0,Doubley=0,Doublelen=0): X (x), Y (y), Len (len) {} pointoperator- (ConstPoint &t)Const{        returnPoint (X-t.x, yt.y); }    int operator*(ConstPoint &t)Const{        Doublec = x*t.y-y*T.x; if(C > EPS)return 1; if(Fabs (c) <eps)return 0; return-1; }};structwall{Point A, B; Wall (Point A=0, point b=0): A (a), B (b) {}};BOOLCanLine (Point A, point B, Wall w[],intN) {     for(intI=0; i<n; i++)    {        if(W[i]. a.x < b.x | | W[i]. a.x >a.x)Continue; intt = (A-B) * (W[i]. A-B) + (A-B) * (W[i]. Bb); if(T = =0)            return false; }    return true;}intMain () {intM;  while(SCANF ("%d", &m)! = EOF && M! =-1)    {        intI, J, nw=0, np=1; DoubleX, y[Ten]; Wall W[MAXN];        Point P[MAXN]; p[0] = Point (0,5,0);  while(m--) {scanf ("%LF%LF%LF%LF%LF", &x, &y[0], &y[1], &y[2], &y[3]); P[NP+ +] = Point (x, y[0], oo), p[np++] = point (x, y[1], oo); P[NP+ +] = Point (x, y[2], oo), p[np++] = point (x, y[3], oo); W[NW+ +] = Wall (Point (X,-1), point (X, y[0])); W[NW+ +] = Wall (Point (X, y[1]), point (X, y[2])); W[NW+ +] = Wall (Point (X, y[3]), point (X, One)); } P[NP+ +] = Point (Ten,5, OO);  for(i=1; i<np; i++)         for(j=0;  j<i; J + +) {Point T= P[i]-P[j]; T.len= sqrt (t.x*t.x+t.y*t.y); if(P[i].len > T.len + p[j].len && CanLine (P[i], P[j], W, nw) = =true) P[i].len= T.len +P[j].len; } printf ("%.2f\n", p[np-1].len); }    return 0;}

The Doors-poj 1556 (segment intersection)

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.