Zoj1158 determines that two line segments are completely intersecting

Source: Internet
Author: User
A square ancient cemetery has n walls, and the endpoints of the walls are all on the side of the square. Given the location of the tombstone (x, y), you must open at least a few doors from the outside until it reaches the tombstone. In addition, the door must be located at the midpoint of the current segment. The idea is clever, because it is impossible to "Bypass" the wall from one point to the end, and it can only be passed over, so it doesn't matter whether the door is opened at the midpoint.

A square ancient cemetery has n walls, and the endpoints of the walls are all on the side of the square. Given the location of the tombstone (x, y), you must open at least a few doors from the outside until it reaches the tombstone. In addition, the door must be located at the midpoint of the current segment. The idea is clever, because it is impossible to "Bypass" the wall from one point to the end, and it can only be passed over, so it doesn't matter whether the door is opened at the midpoint.

A square ancient cemetery has n walls, and the endpoints of the walls are all on the side of the square. Given the location of the tombstone (x, y), you must open at least a few doors from the outside until it reaches the tombstone. In addition, the door must be located at the midpoint of the current segment.

The train of thought is clever, because it is impossible to "Bypass" the wall from one point to the end, and it can only be passed over. It doesn't matter whether the door is opened at the midpoint, only the minimum number of intersection points between the midpoint and the endpoint of the four-week line segment and the wall are required. Furthermore, you only need to determine the minimum intersection of all points and endpoints of the surrounding wall and the interior wall plus one.


Const double eps = 1e-8; double add (double x, double y) {if (fabs (x + y) <eps * (fabs (x) + fabs (y ))) return 0; return x + y;} struct Point {double x, y; Point () {} Point (double _ x, double _ y): x (_ x ), y (_ y) {} Point operator + (Point o) {return Point (add (x, o. x), add (y, o. y);} Point operator-(Point o) {return Point (add (x,-o. x), add (y,-o. y);} Point operator * (double o) {return Point (x * o, y * o);} double operator ^ (Point o) {return add (x * o. y,-y * o. x);} double dist (Point o) {return sqrt (x-o.x) * (x-o.x) + (y-o.y) * (y-o.y);} void read () {scanf ("% lf", & x, & y) ;}}; int interp (Point p1, Point p2, Point q1, Point q2) {double d1 = (p2-p1) ^ (q1-p1); double d2 = (p2-p1) ^ (q2-p1); double d3 = (q2-q1) ^ (p1-q1); double d4 = (q2-q1) ^ (p2-q1); return d1 * d2 <0 & d3 * d4 <0 ;} struct Line {Point s, t; Line () {} Line (Point _ s, Point _ t): s (_ s), t (_ t) {} int intersect (Line o) {// whether the return interp (s, t, O. s, o. t);} void read () {s. read (), t. read ();} friend bool operator <(const Line A, const Line B) {return. s. x <B. s. x ;}}; vector
 
  
Lisline; vector
  
   
Lispoint; int main () {int t, k, n, I, j, T = 1; Point ed, ls, ld; cin> t; while (t --) {cin> n; lispoint. clear (); lisline. clear (); lispoint. push_back (Point (0.0, 0.0); lispoint. push_back (Point (0.0, 100.0); lispoint. push_back (Point (100.0, 0.0); lispoint. push_back (Point (100.0, 100.0); for (I = 1; I <= n; I ++) {ls. read (), ld. read (); lispoint. push_back (ls); lispoint. push_back (Ld); lisline. push_back (Line (ls, ld);} ed. read (); int ans = 100000000, sum; for (I = 0; I <lispoint. size (); I ++) {Line now = Line (lispoint [I], ed); sum = 0; for (j = 0; j <lisline. size (); j ++) {if (now. intersect (lisline [j]) sum ++;} ans = min (ans, sum);} if (T! = 1) puts (""); T ++; printf ("Number of doors = % d \ n", ans + 1);} 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.