POJ 1066 Treasure Hunt (segment intersection)

Source: Internet
Author: User

Test instructions: give you a 100*100 square, and then give you the N line (wall), ensure that the line segment must be within the square and the end of the square boundary (outside the wall), and finally give you a square inside the point (guarantee no longer the wall)

Tell you that the walls (including the outer walls) are surrounded by small rooms, and in small rooms you can walk through the wall from the midpoint of the room (wall) , asking you to walk from a given point to the outside wall at least the number of walls you walk through.

Note: Notice that we can walk out of the midpoint of each room's wall, not the midpoint of a whole line (wall) ....

Then we can find the connection between each point in the perimeter and the given point, and the least intersection with the given segment is the answer.

But each point of the boundary is infinitely multiple, so we can do this: enumerate the two endpoints of each given segment plus the four endpoints of the outer wall

Because the point between every two points on the outer wall is unlikely to bypass the other walls.

#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<iomanip>#include<stdlib.h>#include<iostream>#include<algorithm>using namespacestd;#defineEPS 1E-8/*Note that there may be output -0.000*/#defineSGN (x) (X<-eps -1:x<eps? 0:1)//X is a comparison of two floating-point numbers, note the return integer#defineCvs (x) (x > 0.0 x+eps:x-eps)//floating point Conversion#defineZero (x) (((x) >0? ( x):-(x)) <eps)//determine if it equals 0#defineMul (A, B) (a<<b)#defineDir (A, B) (a>>b)typedefLong Longll;typedef unsignedLong Longull;Const intinf=1<< -;Constll inf=1ll<< -;Const DoublePi=acos (-1.0);Const intmod=1e9+7;Const intmax= -;structpoint{Doublex, y;}; Point Poi[max],enn;DoubleXmult (Point p1,point p2,point p0)//Calculate cross Product (P1-P0) x (p2-p0){    return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y);}intisintersected (Point p1,point p2,point l1,point L2)//segment Intersection (excluding endpoint coincident){    return(Max (p1.x,p2.x) >=min (l1.x,l2.x)) &&(Max (P1.Y,P2.Y)>=min (L1.Y,L2.Y)) &&(Max (l1.x,l2.x)>=min (p1.x,p2.x)) &&(Max (L1.Y,L2.Y)>=min (P1.Y,P2.Y)) &&(Xmult (L1,P2,P1)*xmult (P2,L2,P1) >0) &&(Xmult (P1,L2,L1)*xmult (L2,P2,L1) >0) ;}intSolve (intN) {    intminx=Inf;  for(intI=0; i<n; ++i)//Enumerate each endpoint    {        intres=1;  for(intj=0; j<n-4; j+=2)//each wall to find out if there is an intersection        {            if(Isintersected (poi[i],enn,poi[j],poi[j+1]) Res++; } Minx=min (minx,res); }    if(!N)return 1; returnMinx;}intMain () {intN;  while(~SCANF ("%d",&N)) {n<<=1;  for(intI=0; i<n; ++i) {scanf ("%LF%LF",&poi[i].x,&poi[i].y); } scanf ("%LF%LF",&enn.x,&ENN.Y); poi[n].x=0, poi[n++].y=0; poi[n].x=0, poi[n++].y= -; poi[n].x= -, poi[n++].y=0; poi[n].x= -, poi[n++].y= -;//Add an outer wall four pointsprintf"Number of doors =%d\n", Solve (n)); }    return 0;}

POJ 1066 Treasure Hunt (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.