POJ 2653 Pick-up Sticks "segment intersection"

Source: Internet
Author: User

Test instructions: N sticks are randomly placed on a flat surface, asking what sticks are on top.

Idea: line intersection, because the topic said there are up to 1000 at the top. So go from back to front until the stick is gone or the top stick has a total of more than 1000.

#include <stdio.h>#include<string.h>#include<math.h>#include<iostream>using namespacestd;Const intn=1e5+111; Const Doubleeps=1e-8;intSgnDoublex) {    if(Fabs (x) <eps)return 0; if(x>0)return 1; return-1;}structpoint{Doublex, y; Point () {}, point (DoubleX_Doubley_) {x=x_,y=Y_; } Pointoperator-(ConstPoint &b)Const{        returnPoint (x-b.x,y-b.y); }    Double operator*(ConstPoint &b)Const{        returnx*b.x+y*b.y; }    Double operator^(ConstPoint &b)Const{        returnx*b.y-y*b.x; }};structline{Point S,e; Line () {} line (point S_,point e_) {s=s_,e=E_; }}li[n];DoubleCal (Point p0,point p1,point p2) {//Cross Product    return(p1-p0) ^ (p2-p0);}intXJ (line A,line b) {//determine if two segments intersectPoint a=a.s,b=a.e,c=b.s,d=B.E; returnMax (a.x,b.x)>=min (c.x,d.x) &&Max (c.x,d.x)>=min (a.x,b.x) &&Max (A.Y,B.Y)>=min (C.Y,D.Y) &&Max (C.Y,D.Y)>=min (A.Y,B.Y) &&SGN (Cal (a,c,d))*SGN (Cal (b,c,d)) <=0&&SGN (Cal (c,a,b))*SGN (Cal (d,a,b)) <=0;}intAns[n];intMain () {intN,i,j,js;  while(~SCANF ("%d", &n) &&N) {        Doublex1,x2,y1,y2;js=0;  for(i=1; i<=n;i++) {scanf ("%LF%LF%LF%LF",&x1,&y1,&x2,&y2); Li[i]=Line (Point (X1,y1), point (X2,y2)); }         for(i=n;i&&js< +; i--){             for(j=i+1; j<=n;j++){                if(XJ (LI[I],LI[J))) Break; }            if(j>n) ans[++js]=i; } printf ("Top Sticks:");  for(i=js;i;i--) {printf ("%d%c", ans[i],i==1?'.':','); } puts (""); }    return 0;}

POJ 2653 Pick-up Sticks "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.