poj Pick-up sticks(判断线段相交)

 

Pick-up sticks
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 11537   Accepted: 4337

Description

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.

Input

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

Output

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.

The picture to the right below illustrates the first case from input. poj Pick-up sticks(判断线段相交)_第1张图片

Sample Input

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0

Sample Output

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.

Source

Waterloo local 2005.09.17

 

【思路】

       暴力枚举+判断线段是否相交。

       数据弱得一 哔~  <_<

 

【代码】

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 6 using namespace std;
 7 
 8 const int N = 100000+10;
 9 const double eps = 1e-8;
10 int dcmp(double x) {
11     if(x<eps) return 0; else return x<0? -1:1;
12 }
13 
14 struct Pt {
15     double x,y;
16     Pt (double x=0,double y=0):x(x),y(y) {};
17 };
18 struct Line { Pt a1,a2;
19 };
20 typedef Pt vec;
21 vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
22 
23 double cross(vec A,vec B) { return A.x*B.y-A.y*B.x; }
24 
25 bool SegInter(Pt s1, Pt e1, Pt s2, Pt e2) {
26     if( min(s1.x, e1.x) <= max(s2.x, e2.x) &&
27         min(s1.y, e1.y) <= max(s2.y, e2.y) &&
28         min(s2.x, e2.x) <= max(s1.x, e1.x) &&
29         min(s2.y, e2.y) <= max(s1.y, e1.y) &&
30         cross(e1-s1,s2-s1) * cross(e1-s1,e2-s1) <= 0 &&
31         cross(e2-s2,s1-s2) * cross(e2-s2,e1-s2) <= 0 
32       ) return true;
33     return false;
34 }
35 
36 int n;
37 Line L[N];
38 
39 int main() {
40     while(scanf("%d",&n)==1 && n) {
41         double x,y,x2,y2;
42         FOR(i,1,n) {
43             scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
44             L[i].a1=Pt(x,y) , L[i].a2=Pt(x2,y2);
45         }
46         bool first=1;
47         printf("Top sticks:");
48         FOR(i,1,n) {
49             bool flag=1;
50             FOR(j,i+1,n)
51                 if(SegInter(L[i].a1,L[i].a2,L[j].a1,L[j].a2)) {
52                     flag=0; break;
53                 }
54             if(flag) {
55                 if(first) first=0; else putchar(',');
56                 printf(" %d",i);
57             }
58         }
59         puts(".");
60     }
61     return 0;
62 }

 

你可能感兴趣的:(poj Pick-up sticks(判断线段相交))