POJ 2653 Pick-up sticks

蒻苣:弱弱很弱,路过的巨巨还不吝赐教!^.^...QAQ

题意:给你n个线段,求最上面的线段有多少个(没有被后面添加的线段相交)

题解:直接暴力即可。对于当前第i个线段,枚举i+1~n-1 是否有线段与其相加 有的话这条线段就是被覆盖的,也就是不要输出的

代码:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <cmath>
 8 using namespace std;
 9 const double INF  = 1E200;
10 const double wc = 1E-8;
11 struct POINT{
12         double x,y;
13         POINT( double a = 0,double b = 0 )  { x = a; y = b; }
14 };
15 struct LINESEG{
16         POINT e;
17         POINT s;
18         LINESEG( POINT x,POINT y ) { e = x; s = y; }
19         LINESEG(){}
20 };
21 LINESEG L[100000+10];
22 double dist(POINT p1,POINT p2)              
23 {
24  return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );
25 }
26 double multiply(POINT sp,POINT ep,POINT op)
27 {
28  return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
29 }
30 bool online(LINESEG l,POINT p)
31 {
32  return( (multiply(l.e,p,l.s)==0) &&( ( (p.x-l.s.x)*(p.x-l.e.x)<=0 )&&( (p.y-l.s.y)*(p.y-l.e.y)<=0 ) ) );
33 }
34 bool intersect(LINESEG u,LINESEG v)
35 {
36  return( (max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&&                     
37    (max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
38    (max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
39    (max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
40    (multiply(v.s,u.e,u.s)*multiply(u.e,v.e,u.s)>=wc)&&         
41    (multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=wc));
42 }
43 bool intersect_A(LINESEG u,LINESEG v)
44 {
45  return ((intersect(u,v))&&
46    (!online(u,v.s))&&
47    (!online(u,v.e))&&
48    (!online(v,u.e))&&
49    (!online(v,u.s)));
50 }
51 bool intersect_l(LINESEG u,LINESEG v)
52 {
53  return multiply(u.s,v.e,v.s)*multiply(v.e,u.e,v.s)>=wc;
54 }
55 int vis[100000+50];
56 int main()
57 {
58         int n;
59         while(cin>>n)
60         {
61                 if(!n) break;
62                 memset(vis,0,sizeof(vis));
63                 for(int i = 0;i < n; i++)
64                         scanf("%lf%lf%lf%lf",&L[i].e.x,&L[i].e.y,&L[i].s.x,&L[i].s.y);
65                 for(int i = 0;i < n; i++)
66                         for(int j = i+1;j < n; j++)
67                                 if(intersect_A(L[i],L[j]))
68                                         {vis[i]=1;break;}
69                 int flag = 0;
70                 printf("Top sticks:");
71                 for(int i = 0;i < n;i++)
72                 {
73                         if(vis[i]==0){
74                                 if(flag==0)
75                                 {printf(" %d",i+1);flag=1;}
76                                 else
77                                 printf(", %d",i+1);
78                         }
79                 }
80                 puts(".");
81         }
82 }
View Code

 

posted on 2015-08-21 23:16  小松song  阅读(94)  评论(0)    收藏  举报

导航