poj 2653 Pick-up sticks

题意:
按顺序给出线段, 上面的可能会覆盖下面的, 求出不被其他线段覆盖的所有线段


分析:暴力搜索,如果一条线段不被后面任一条覆盖,则它是答案之一


重点:线段的平行,相交,点与线段的位置关系的判定, 点积叉积的灵活运用。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#define MAX 100005
#include <iostream>
#define PI acos(-1.0)
#define eps 1e-5
#define INF 1e8

using namespace std;
inline int sgn(double x){ return x < -eps ? -1 : ( x > eps ? 1 : 0) ;}
struct Point
{
    double x, y;
    Point(){}
    Point(double xx, double yy):x(xx), y(yy){}
    Point operator - (const Point &p)const{
        return Point(x - p.x, y - p.y);
    }
    void input(){
        cin>>x>>y;
    }
    void output(){
        cout<<x<<y<<endl;
    }

    friend double det(const Point &p1, const Point &p2){
        return p1.x * p2.y - p1.y * p2.x;
    }
    friend double dot(const Point &p1, const Point &p2){
        return p1.x *p2.x + p1.y * p2.y;
    }
    friend Point operator * (const Point &p1, const double &w){
        return Point(p1.x * w, p1.y * w);
    }
    friend Point operator / (const Point &p1, const double &w){
        return Point(p1.x/ w, p1.y/ w);
    }
};
struct Line
{
    Point s, t;
    Line(){}
    Line(const Point &p1, const Point &p2):s(p1), t(p2){}
    void input(){
        s.input();
        t.input();
    }
    bool isParell(const Line &line) const{
        if(sgn( det(line.t - line.s, t - s) ) == 0 ){
            return true;
        }
        return false;
    }

    bool InsectPoint(Point &res, const Line &line)const{
            if(isParell(line))return false;
            double s1  =  det( t - s, line.s - s);
            double s2  =  det( t - s, line.t - s);
            res = (line.t * s1 - line.s * s2) / (s1 - s2);
            return true;
    }
    bool isOnLine(const Point &p1)const{//包括两端
        return ( sgn( det(p1 - s, t - s) ) == 0 && sgn(dot(p1 - s, p1 - t) ) <= 0);
    }
    bool isInsect(const Line &line)const{
        Point res;
        if(!InsectPoint(res, line)){
//            puts("parell");
            return false;
        }
//        puts("not parell");
//        res.output();
        return (isOnLine(res) && line.isOnLine(res));
    }
}line[MAX];
int main()
{
    int n;
    while(cin>>n, n){
        for(int i = 0; i < n;i++){
            line[i].input();
        }
//        cout<<line[0].isInsect(line[1])<<endl;
        printf("Top sticks: ");
        vector<int>ans;
        for(int i = 0; i < n; i++){
            bool isFind = true;
            for(int j = i + 1; j < n; j++){
                if(line[i].isInsect(line[j])){
                    isFind = false;
                    break;
                }
            }
            if(isFind)ans.push_back(i + 1);
        }
        int tSize = ans.size();
        for(int i = 0; i < tSize - 1; i++){
            printf("%d, ", ans[i]);
        }printf("%d.\n", ans[tSize - 1]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值