Pick-up sticks POJ - 2653

时间:2020-01-14
本文章向大家介绍Pick-up sticks POJ - 2653,主要包括Pick-up sticks POJ - 2653使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Pick-up sticks

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

 题目:

      

 

 

 

 

 

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

 

 1 // 
 2 // Created by HJYL on 2020/1/13.
 3 //
 4 #include<iostream>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<queue>
 8 #include<cmath>
 9 #define eps 1e-8
10 #define Inf 0x7fffffff
11 //#include<bits/stdc++.h>
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<T;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<<p[i].x<<" "<<p[i].y<<endl;
56         for(int i=0;i<T;i++)
57         {
58             for(int j=i+1;j<T;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<T;i++)
71             if(!flag[i]) gg[re++]=i+1;
72         printf("Top sticks:");
73         for(int i=0;i<re-1;i++)
74             printf(" %d,",gg[i]);
75         printf(" %d.\n",gg[re-1]);
76     }
77     return 0;
78 }

$flag 上一页 下一页