Poj 1066 treasure hunt

Source: Internet
Author: User

Poj_1066

A more intuitive idea is to regard each room as a point, each door as an edge, and then go to BFs to solve the problem. However, it is very difficult to determine the room, and it is difficult to use effective means to mark these rooms one by one.

As a result, we may wish to transform our thinking into a single point. The door is still relatively easy to find. For any straight line, all the intersection points on it are found, and the midpoint of the adjacent two intersections is naturally the door. In this way, the other question is how to construct the edges between these doors? We can connect two doors at will. It is not difficult to find that if these two doors can be directly reached, the line segments formed by the links between the two doors will not form a standard intersection with other lines, if the two doors cannot be directly reached, at least a regular intersection will be formed with a line segment.

Therefore, we have thought 1:

First, enumerate the line segments, locate each intersection point on this line segment, and then the midpoint between two adjacent intersections is a door. In this way, all the doors are found first. Then, determine whether the line segment formed by the line between the two doors will form a standard intersection with other line segments to construct the edge between the two doors. Finally, we start BFS from the starting point, and end BFS when there is a point on four sides.

At the same time, there is another simpler way of thinking:

If we connect the starting point to a door on the four sides, that is, a final exit, a line segment will be found to be intersecting with some lines, if you want to exit from this exit, then these line segments (refer to the long line segments with endpoints on the four sides) it is inevitable to pass through (it is easier to understand by combining images ). Therefore, the number of these line segments plus 1 is the minimum number of doorways to exit from this exit. Therefore, we can enumerate the four sides of the door as the exit, and then judge one by one the starting point and the number of other lines connected to these exits intersection, finding the minimum value of this quantity plus 1 is the final result.

The followingProgramIt is a program of thought 1.

# Include <stdio. h>
# Include < String . H>
# Include <stdlib. h>
# Define Maxd 50
# Define Maxp 1010.
# Define Maxm 1000010
# Define Zero 1e-8
Int N, first [maxp], next [maxm], E, V [maxm], R [maxd], p, cur, d [maxp], Q [maxp];
Double SX, Sy, X1 [maxd], X2 [maxd], Y1 [maxd], Y2 [maxd], TX [maxd], Ty [maxd], X [maxp], Y [maxp];
Int CMP ( Const Void * _ P, Const Void * _ Q)
{
Int * P = ( Int *) _ P;
Int * Q = ( Int *) _ Q;
Double T1 = (TX [* p]-X1 [cur]) * (TX [* p]-X1 [cur]) + (TY [* p]-Y1 [cur]) * (TY [* p]-Y1 [cur]);
Double T2 = (TX [* q]-X1 [cur]) * (TX [* q]-X1 [cur]) + (TY [* q]-Y1 [cur]) * (TY [* q]-Y1 [cur]);
Return T1 <t2? - 1 : 1 ;
}
Double FABS ( Double X)
{
Return X < 0 ? -X: X;
}
Int DCMP ( Double X)
{
If (FABS (x) <zero)
Return 0 ;
If (X < 0 )
Return - 1 ;
Return 1 ;
}
Void Init ()
{
Int I, J, K;
For (I = 0 ; I <n; I ++)
Scanf ( " % Lf " , & X1 [I], & Y1 [I], & X2 [I], & Y2 [I]);
Scanf ( " % Lf " , & Sx, & Sy );
X1 [N] = 0 , Y1 [N] = 0 , X2 [N] = 0 , Y2 [N] = 100 ;
++ N;
X1 [N] = 0 , Y1 [N] = 0 , X2 [N] = 100 , Y2 [N] = 0 ;
++ N;
X1 [N] =100 , Y1 [N] = 0 , X2 [N] = 100 , Y2 [N] = 100 ;
++ N;
X1 [N] = 0 , Y1 [N] = 100 , X2 [N] = 100 , Y2 [N] = 100 ;
++ N;
P = 0 ;
X [p] = Sx, Y [p] = sy;
++ P;
}
Double Det ( Double X1, Double Y1, Double X2, Double Y2)
{
Return X1 * Y2-X2 * Y1;
}
Double Cross ( Int I, Double X, Double Y)
{
Return Det (X2 [I]-X1 [I], Y2 [I]-Y1 [I], X-X1 [I], Y-Y1 [I]);
}
Int Check ( Int I, Int J)
{
Int K;
Double T1, T2, T3, T4;
For (K = 0 ; K <n; k ++)
{
T1 = det (X2 [k]-X1 [K], Y2 [k]-Y1 [K], X [I]-X1 [K], Y [I]-Y1 [k]);
T2 = det (X2 [k]-X1 [K], Y2 [k]-Y1 [K], X [J]-X1 [K], Y [J]-Y1 [k]);
T3 = det (X [J]-X [I], Y [J]-y [I], X1 [k]-X [I], y1 [k]-y [I]);
T4 = det (X [J]-X [I], Y [J]-y [I], X2 [k]-X [I], y2 [k]-y [I]);
If (DCMP (T1) * DCMP (T2) < 0 & DCMP (T3) * DCMP (T4) < 0 )
Return 0 ;
}
Return 1 ;
}
Void Add ( Int X, Int Y)
{
V [e] = y;
Next [e] = first [x];
First [x] = E;
++ E;
}
Void Build ()
{
Int I, J, K;
E = 0 ;
Memset (first ,- 1 , Sizeof (First ));
For (I = 0 ; I <p; I ++)
For (J = I + 1 ; J <p; j ++)
If (Check (I, j ))
{
Add (I, j );
Add (J, I );
}
}
Void Prepare ()
{
Int I, J, K, NK, T;
Double T1, T2, T3, T4;
For (I = 0 ; I <n; I ++)
{
T = 0 ;
For (J = 0 ; J <n; j ++)
If (J! = I)
{
T1 = cross (I, X1 [J], Y1 [J]);
T2 = cross (I, X2 [J], Y2 [J]);
T3 = cross (J, X1 [I], Y1 [I]);
T4 = cross (J, X2 [I], Y2 [I]);
If (DCMP (T1) * DCMP (T2) <= 0 & DCMP (T3) * DCMP (T4) <= 0 )
{
TX [T] = (FABS (T2) * X1 [J] + FABS (T1) * X2 [J])/(FABS (T1) + FABS (T2 ));
Ty [T] = (FABS (T2) * Y1 [J] + FABS (T1) * Y2 [J])/(FABS (T1) + FABS (T2 ));
++ T;
}
}
For (J = 0 ; J <t; j ++)
R [J] = J;
Cur = I;
Qsort (R, T, Sizeof (R [ 0 ]), CMP );
For (J = 0 ; J <t- 1 ; J ++)
{
K = R [J], nk = R [J + 1 ];
If (FABS (TX [k]-TX [NK]) <zero & FABS (TY [k]-ty [NK]) <zero)
Continue ;
X [p] = (TX [k] + Tx [NK])/ 2 , Y [p] = (TY [k] + ty [NK])/ 2 ;
++ P;
}
}
Build ();
}
Void Solve ()
{
Int I, J, K, U, front, rear;
Prepare ();
Front = rear = 0 ;
Memset (D, 0x3f ,Sizeof (D ));
D [ 0 ] = 0 ;
Q [rear ++] = 0 ;
While (Front <rear)
{
U = Q [Front ++];
If (FABS (X [u]) <zero | FABS (X [u]- 100 ) <Zero | FABS (Y [u]) <zero | FABS (Y [u]- 100 ) <Zero)
Break ;
For (I = first [u]; I! =- 1 ; I = next [I])
If (D [u] + 1 <D [V [I])
{
D [V [I] = d [u] + 1 ;
Q [rear ++] = V [I];
}
}
Printf ( " Number of doors = % d \ n " , D [u]);
}
Int Main ()
{
While (Scanf ( " % D " , & N) = 1 )
{
Init ();
Solve ();
}
Return 0 ;
}


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.