Pick-up sticks POJ - 2653

Pick-up sticks

 题目链接:https://vjudge.net/problem/POJ-2653

 题目:

      Pick-up sticks POJ - 2653_第1张图片

 

Pick-up sticks POJ - 2653_第2张图片

 

 Pick-up sticks POJ - 2653_第3张图片

 

 

 思路:暴力判断两线段间是否相交,若相交则去掉前面的线段,将对应棍子的编号输出即可

 

 1 // 
 2 // Created by HJYL on 2020/1/13.
 3 //
 4 #include
 5 #include
 6 #include
 7 #include
 8 #include
 9 #define eps 1e-8
10 #define Inf 0x7fffffff
11 //#include
12 using namespace std;
13 const int maxn=1e5+100;
14 struct Point{
15     double x,y;
16 };
17 double min(double a, double b) { return a < b ? a : b; }
18 
19 double max(double a, double b) { return a > b ? a : b; }
20 double Cross(Point &sp, Point &ep, Point &op)
21 {
22     return (sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y);
23 }
24 bool IsSegmentIntersect(Point a, Point b, Point c, Point d)
25 {
26     if( min(a.x, b.x) > max(c.x, d.x) ||
27         min(a.y, b.y) > max(c.y, d.y) ||
28         min(c.x, d.x) > max(a.x, b.x) ||
29         min(c.y, d.y) > max(a.y, b.y) )
30         return 0;
31 
32     double h, i, j, k;
33 
34     h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
35     i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
36     j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
37     k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
38 
39     return h * i <= eps && j * k <= eps;
40 }
41 
42 int main()
43 {
44     int T;
45     bool flag[maxn];
46     while(~scanf("%d",&T)) {
47         memset(flag,0, sizeof(flag));
48         if(T==0)
49             break;
50         Point p[maxn],b[maxn];
51         for(int i=0;i){
52             scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &b[i].x, &b[i].y);
53         }
54         //for(int i=0;i<=pos;i++)
55         // cout<
56         for(int i=0;i)
57         {
58             for(int j=i+1;j)
59             {
60 
61                     if(IsSegmentIntersect(p[i],b[i],p[j],b[j]))
62                     {
63                         flag[i]=1;
64                         break;
65                     }
66             }
67         }
68         int gg[maxn]={0};
69         int re=0;
70         for(int i=0;i)
71             if(!flag[i]) gg[re++]=i+1;
72         printf("Top sticks:");
73         for(int i=0;i1;i++)
74             printf(" %d,",gg[i]);
75         printf(" %d.\n",gg[re-1]);
76     }
77     return 0;
78 }

你可能感兴趣的:(Pick-up sticks POJ - 2653)