You can Solve a Geometry problem too
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8693 Accepted Submission (s): 4232
Problem Descriptionmany Geometry (geometry) problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many acmers, geometry problems was always much trouble, but this problem was very easy, afte R all we is now attending an exam, not a contest:)
Give you N (1<=n<=100) segments (line segment), please output the number of all intersections (intersection). You should count repeatedly if M (m>2) segments intersect on the same point.
Note:
You can assume that, segments would not intersect at the more than one point.
Inputinput contains multiple test cases. Each test case is contains a integer n (1=n<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which is coordinates of the segment ' s ending.
A test case, starting with 0 terminates, the input and this test are not processed.
Outputfor, print the number of intersections, and one line one case.
Sample Input20.00 0.00 1.00 1.000.00 1.00 1.00 0.0030.00 0.00 1.00 1.000.00 1.00 1.00 0.0000.00 0.00 1.00 0.000
Sample Output13
Authorlcy only used the Intersect segment template
1#include <iostream>2#include <cmath>3 4 using namespacestd;5 6 struct Point7 {8 Doublex;9 Doubley;Ten }; One structlineseg A { - Point S; - Point E; the }; - - DoubleMultiply (point a,point b,point c) - { + return((b.x-a.x) * (C.Y-A.Y)-(c.x-a.x) * (b.y-a.y)); - } + BOOLintersect (lineseg u,lineseg v) A { at return(Max (u.s.x,u.e.x) >=min (v.s.x,v.e.x)) &&//rejection Experiment -(Max (v.s.x,v.e.x) >=min (u.s.x,u.e.x)) && -(Max (U.S.Y,U.E.Y) >=min (v.s.y,v.e.y)) && -(Max (V.S.Y,V.E.Y) >=min (u.s.y,u.e.y)) && -(Multiply (V.S,U.E,U.S) *multiply (U.E,V.E,U.S) >=0) &&//Cross-stand experiment -(Multiply (U.S,V.E,V.S) *multiply (V.E,U.E,V.S) >=0)); in } - intMain () to { + - intN; the while(cin>>n&&N) * { $Lineseg a[101];Panax Notoginseng intCount=0; - for(intI=0; i<n;i++) the { +Cin>>a[i].s.x>>a[i].s.y>>a[i].e.x>>a[i].e.y; A } the for(intI=0; i<n;i++) + for(intj=0; j<n;j++) - if(Intersect (A[i],a[j]) &&i!=j) $count++; $cout<<count/2<<Endl; - }48return 0;49}
Hdoj 1086 template water over