Algorithm: poj1066 treasure hunter problem.

Source: Internet
Author: User

 Packagepractice;ImportJava.util.Scanner; Public classTreasurehunt { Public Static voidMain (string[] args) {Scanner cin=NewScanner (system.in); int[] walls; floatx, y; intDoors =Integer.max_value, Temp1, Temp2; intn =Cin.nextint (); Walls=New intN [4];  for(intj = 0; J < N; J + +) {walls[j][0] =Cin.nextint (); walls[j][1] =Cin.nextint (); walls[j][2] =Cin.nextint (); walls[j][3] =Cin.nextint (); } x=cin.nextfloat (); Y=cin.nextfloat ();  for(intj = 0; J < N; J + +) {Temp1= Find (walls[j][0], walls[j][1], x, Y, Walls, j); Temp2= Find (walls[j][2], walls[j][3], x, Y, Walls, j); Doors= Temp1 < Temp2? (Temp1 < doors?)temp1:doors): (Temp2< doors?temp2:doors); }        if(n = = 0) Doors= 0; Doors++; System.out.println ("Number of doors =" +doors); }    Private Static intFindintX1,intY2,floatXfloatYint[] Walls,intj) {intCount = 0;  for(inti = 0, len = walls.length; i < Len; i++) {            if(i = =j)Continue; if(Isintersect (x1, y2, x, Y, Walls[i]) Count++; }        returncount; }    /*** cross-State experiment*/    Private Static BooleanIsintersect (intStartX,intStarty,floatEndX,floatEndY,int[] wall) {        if((Math.max (StartX, EndX) >= math.min (wall[0], wall[2]))                && (Math.max (wall[0], wall[2]) >=math.min (StartX, endx))&& (Math.max (Starty, EndY) >= math.min (wall[1], wall[3]))                && (Math.max (wall[1], wall[3]) >=math.min (Starty, EndY))&& (Multiply (wall[0], wall[1], EndX, EndY, StartX, Starty)* Multiply (EndX, EndY, wall[2], wall[3], StartX, starty) > 0)                && (Multiply (StartX, Starty, wall[2], wall[3], wall[0], wall[1])                        * Multiply (wall[2], wall[3], EndX, EndY, wall[0], wall[1]) > 0))            return true; Else            return false; }    Private Static DoubleMultiplyfloatX1,floatY1,floatX2,floatY2,floatX3,floaty3) {        return((X1-X3) * (y2-y3)-(x2-x3) * (Y1-y3)); }}
Click I expand Code

The problem is:

A gang of treasure-seekers detected the treasures at the bottom of the Egyptian pyramid, but the treasures were surrounded by n walls, and if they were to be blasted, they could only be opened at the midpoint of each wall. Now the problem is, for a random given n wall, calculate the minimum number of open doors required.

The input data is as follows, the first row of integer n represents the number of walls, and the following n lines represent the wall end coordinates, with the lower left corner of the pyramid (0,0) to the upper-right corner (100,100), and four data for each row (X1,Y1,X2,Y2)

The last row of data represents the location of the treasure point P.

7
20 0 37 100
40 0 76 100
85 0 0 75
100 90 0 90
0 71 100 61
0 14 100 38
100 47 47 100
54.5 55.4

The idea of the algorithm is simple (but the implementation is a bit problematic, more time consuming, is being improved):

Use all the points given to the treasure point p to do line segments, to find the lowest intersection of the group, both the minimum number of open doors.

The above conclusions need to prove several inferences, proving the process as follows:

1, for any p, if and p only spaced a wall, then, p only need to open a door

Proof: No need to prove obvious.

Conclusion: The number of doors can be proved as long as the number of walls is proved.

2, any midpoint on the edge P0 to P do line segment, the number of intersections equals p to pi interval minimum wall number

Prove:

Connect the p0-p with the intersection set to N.

, there is a open door path, connected P0 to P, in the middle through the M wall.

It is known that the p0-p and the open path are closed polygons (the path is straight and not discussed with P0-p).

There is theorem one: the smallest complete path does not break through the same wall two times. (if there are two times, then there are at least three intersection a\b\c, connection a-c, the path is less, easy to license)

There is theorem two: there is only one wall between the two sub-paths (easy to pass through the wall can only reach the opposite side, can not reach the same side)

By the theorem two know, the wall will pass through the inside of the polygon, by the theorem one know, the wall will not pass through the path again, then, the wall will go through p0-p

That's m<=n.

  

and axiom One: two segments with at most one intersection

The segments that pass through the p0-p must intersect the enclosing polygons. (Because the line ends fall on the edge of the pyramid, it is easy to license)

namely N<=m

So m=n.

  

  

  

3, any existing segment end point and P connection, the number of intersections equals to the adjacent two midpoint respectively with the P line in the intersection of the small value

Prove:

There are two segment l1,l2 respectively cross the pyramid outer wall for P1,P2, and there is no other existing segment intersection between P1 and P2

With a dot pn in between p1,p2

To Pn-p do wiring, with an existing line LX, and L1 intersect, but do not intersect with pn-p, at this time to meet the PN P of the intersection of less than P1.

At this point, LX and p1-p2 outside the intersection of the wall, must fall between the PN and P1 (this point is easy to pass), and P1, P2 there is no existing segment intersection of the violation.

Can be certified, pn-p of the number of points must be greater than or equal to p1-p.

Similarly, the relationship between PN and P2 can be proven.

When equal, PN is equivalent to P1.

  

When greater than, the inference is as follows:

There is a point px,px on the pn-p1 extension cord for any point on the line segment for the P1-p0 (P0 is another intersection or pyramid Vertex, where the intersection is equal to the vertex).

  

It is known that there is no arbitrary intersection between PX and P1.

Then, with the previous inference, the p1-p is greater than or equal to the px-p0 intersection point.

When greater than, there must be a first-line segment with the p1-p intersect, do not intersect with the PX-P0, this segment can not fall in the P1-PX segment, can only fall in P1-PN, and not P1 itself (intersection non-coincident), and P1-PN no intersection between the violation.

Therefore, must be equal to the relationship (the same can be L1 that is the pn-p more than the intersection of the line)

  

The intersection is equal to the vertex case, if there is a P-point intersection line, the intersection line must have two points of intersection with the pyramid edge, regardless of the landing point, can refer to the first half of the inference to the same.

The certificate is completed.

Conclusion:

The minimum value can be obtained by requiring only the intersection point of the segment endpoint and p.

Algorithm: poj1066 treasure hunter problem.

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.