POJ 1066 Treasure Hunt (computational geometry)

Source: Internet
Author: User
Tags x2 y2

Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5857 Accepted: 2439

Description

Archeologists from the Antiquities and Curios Museum (ACM) has flown to Egypt to examine the Great Pyramid of Key-ops. Using State-of-the-art Technology They is able to determine that the lower floor of the pyramid was constructed from a ser ies of StraightLine walls, which intersect to form numerous enclosed chambers. Currently, no doors exist to allow access to any chamber. This State-of-the-art technology have also pinpointed the location of the treasure. What these dedicated (and greedy) archeologists want to do are blast doors through the walls to get to the treasure. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for Dyna Mite) They want to blast through the minimum number of doors. For structural integrity purposes, doors should is blasted at the midpoint of the wall of the "the" being entered. You is to write a program which determines this minimum number of doors.
An example is shown below:

Input

The input would consist of one case. The first line would be a integer n (0 <= n <=) specifying number of interior walls, followed by n lines Containi ng integer endpoints of each wall x1 y1 x2 y2. The 4 enclosing walls of the pyramid has a fixed endpoints at (0,0); (0,100); (100,100) and (100,0) and is not included in the list of walls. The interior walls always spans from one exterior wall to another exterior wall and is arranged such that no more than Walls intersect at any point. Assume that no, given walls coincide. After the listing of the interior walls there would be a final line containing the floating point coordinates of the Trea Sure in the treasure-guaranteed (lie on a wall).

Output

Print a single line listing the minimum number of doors which need to is created, in the format shown below.

Sample Input

Sample Output

Source

East Central North America 1999

Test instructions: There are some staggered inner walls inside a square fence, the inner wall ends are on a square, there is a point inside the square, the minimum number of doors to go from outside the square to this point, the door can only be the focus of the line segment.

Solution: Enumerate the doors on the four-way wall, the answer is that the line between the midpoint of the door and the starting point intersects with the least of all the lines.

#include <cstring> #include <algorithm> #include <cmath> #include <iostream> #include < cstdio> #include <vector> #define N 100010#define inf 99999999999.0using namespace std;struct point {double x, y; }; struct line {point p1,p2;}; int N,num;bool vis[n];vector<line>vec;vector<double>p[4];    Point Addpoint (double x,double y) {point it;    It.x=x;    It.y=y; return it;}    void AddLine (double x1,double y1,double x2,double y2) {point a=addpoint (x1,y1);    Point B=addpoint (X2,y2);    Line it;    It.p1=a,it.p2=b; Vec.push_back (it);} Cross product double multi (point p0,point p1,point p2) {return (p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y-P0.Y);}           Segment intersection judgment BOOL Is_inter (point S1,point e1,point s2,point E2) {return (max (s1.x,e1.x) >=min (s2.x,e2.x)) && (Max (s2.x,e2.x) >=min (s1.x,e1.x)) && (Max (S1.Y,E1.Y) >=min (S2.Y,E2.Y)) && (max (S2.Y,E2.Y) >=min (S1.Y,E1.Y)) && (multi (s1,s2,e1) *multi (s1,e1,e2) >=0) && (multi (s2,s1,e2) *multi (s2,e2,e1) >=0);}    Four points on the edge of void P_push (double x,double y) {if (x==0) p[0].push_back (y);    else if (x==100) p[1].push_back (y);    else if (y==0) p[2].push_back (x); else P[3].push_back (x);}    int main () {//Freopen ("Test.in", "R", stdin);        while (cin>>n) {vec.clear ();        for (int i=0; i<4; i++) p[i].clear ();        Double Sx,sy,ex,ey;            for (int i=0; i<n; i++) {scanf ("%lf%lf%lf%lf", &sx,&sy,&ex,&ey);            AddLine (Sx,sy,ex,ey);            P_push (Sx,sy);        P_push (Ex,ey);        } point T;        scanf ("%lf%lf", &AMP;T.X,&AMP;T.Y);        int min=1000000;        int sum=0;            for (int i=0; i<4; i++) {p[i].push_back (0.0);            P[i].push_back (100.0);            Sort (P[i].begin (), P[i].end ());            sum=0;            Point it;               for (int j=1; j<p[i].size (); j + +) {if (i<2) {         On two longitudinal walls it.y= (p[i][j]+p[i][j-1])/2.0;                it.x= (i==0)? 0.0:100;                    } else {///on Transverse wall it.x= (p[i][j]+p[i][j-1])/2.0;                It.y= (i==2)? 0.0:100;                }///The number of intersections with all segments sum=0;                for (int k=0; k<vec.size (); k++) {if (Is_inter (T,IT,VEC[K].P1,VEC[K].P2)) sum++;            } min=min (Sum,min); }} printf ("Number of doors =%d\n", min+1);///Plus last Wall} return 0;


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1066 Treasure Hunt (computational geometry)

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.