Poj_2653 Pick-up sticks -线段相交问题(暴力、链表、模版)

题目:你在一个平面上丢木棍,木棍在顶端的定义是没有木棍在此木棍之上。问最终有多少个木棍在顶端。

吐槽:其实题目没有讲太清楚,好是我想太过呢?首先,题目没讲木棍是否全是规范相交,不规范相交算不算相交等问题都没有在题目上讲清楚,所以只能连蒙带猜去做了。

       题目给了一个挺重要的信息,就是顶端木棍不超过1000,其实这里我还是挺迷惑的,不知是最终结果不超呢,还是过程也不超呢?

/************************************************
Author        :DarkTong
Created Time  :2016/8/4 8:46:36
File Name     :Poj_2653.cpp
*************************************************/

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

#define eps 1e-9
typedef long long LL;
typedef double Db;
struct Point
{
    Db x, y;
    Point(Db x=0, Db y=0):x(x), y(y){}
}C;
typedef Point Vector;
//向量或点的四则运算
Vector operator + (Vector A, Vector B){ return Vector(A.x+B.x, A.y+B.y);}
Vector operator - (Vector A, Vector B){ return Vector(A.x-B.x, A.y-B.y);}
Vector operator * (Vector A, Db p){ return Vector(A.x*p, A.y*p);}
Vector operator / (Vector A, Db p){ return Vector(A.x/p, A.y/p);}
//逻辑判断
bool operator < (const Vector &a, const Vector &b){ return a.x<b.x||a.x==b.x&&a.y<b.y;}
int dcmp(Db x){    if(fabs(x)<eps) return 0; else return x<0 ? -1 : 1;}
bool operator == (const Vector &a, const Vector &b){return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;}
//基本运算
Db Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y;}    /*点积: 角度分布:>0(-90, 90), ==0(90或-90), <0((-180,90)或(90,180])*/
Db Cross(Vector A, Vector B){ return A.x*B.y - A.y*B.x;}    /*叉积(判<180的角): ==0(共线), >0(逆时针), <0(顺时针)  */
double Length(Vector A){ return sqrt(Dot(A, A));}
bool OnSegment(Point p, Point a1, Point a2)
{
    return dcmp(Cross(a1-p, a2-p))==0&&dcmp(Dot(a1-p, a2-p))<0;
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
{
    if(a1==b1&&a2==b2||a1==b2&&a2==b1) return true;
    double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1),
           c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);

    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0||OnSegment(a1,b1,b2)
        ||OnSegment(a2, b1, b2)||OnSegment(b1, a1, a2)||OnSegment(b2, a1, a2);;
}
//判断点是否在线段上
bool SegmentProperIntersectionIncludePoint(Point a1, Point a2, Point b1, Point b2)
{
    return SegmentProperIntersection(a1,a2, b1,b2)||OnSegment(a1,b1,b2)
        ||OnSegment(a2, b1, b2)||OnSegment(b1, a1, a2)||OnSegment(b2, a1, a2);
}
/*******************************************************************************/
const int maxn = 100000 + 10;
Point line[maxn][2];
int next[maxn], val[maxn], rec[maxn], sz, n, rn;
void add(int th)
{
    next[sz]=next[0];
    val[sz]=th;
    next[0]=sz;
    sz++;
}
void erase(int th)
{
    next[th] = next[next[th]];
}
void solve()
{
    memset(next, -1, sizeof(next));
    sz=1;
    for(int i=1;i<=n;++i)
    {
        for(int j=0;next[j]!=-1;)
        {
            int a = next[j], b=i;
            if(SegmentProperIntersection(line[a][0], line[a][1], line[b][0], line[b][1]))
                erase(j);
            else j=next[j];
        }
        add(i);
    }
}


int main()
{
    int T, cas=1;
    while(scanf("%d", &n)==1&&n)
    {
        for(int i=1;i<=n;++i) scanf("%lf%lf%lf%lf", &line[i][0].x, &line[i][0].y, &line[i][1].x, &line[i][1].y);
        solve();
        rn=0;
        for(int i=next[0];i!=-1;i=next[i])
            rec[rn++]=val[i];
        printf("Top sticks:");
        for(int i=rn-1;i>=0;--i)
        {
            if(i!=rn-1) printf(",");
            printf(" %d", rec[i]);
        }
        puts(".");


    }
    
    return 0;
}

转载于:https://www.cnblogs.com/DarkTong/p/5735620.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值