• UVa 10902


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <vector>
    using namespace std;
    #define MAXN 100002
    struct point {
        double x,y;
        point (double x = 0 ,double y = 0){
            this->x = x;
            this->y = y;
        }
    };
    struct line{
        point st,en;
        line(point st,point en){
            this->st = st;
            this->en = en;
        }
    };
    class Isolated{
        private:
            int segNum;
            vector<line> segments;
            int ans[MAXN];
            int ansNum;
        public:
            void init();
            void readData(int num);
            void process();
            bool isCross(line la,line lb);
            bool isInSeg(point a,point la,point lb);//点是否在线段l上
            long long dir(point i,point j,point k);//向量叉积
    };
    void Isolated::init(){
        ansNum = 0;
        segments.clear();
    }
    void Isolated::readData(int num){
        segNum = num;
        double x1,y1,x2,y2;
        while(num--){
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            point st(x1,y1),en(x2,y2);
            line la(st,en);
            segments.push_back(la);
        }
    }
    void Isolated::process(){
        bool isColl;//线段是否是孤立的
        for(int i = 0;i < segNum;i++){
            isColl = true;
            for(int j = segNum - 1;j >= 0;j--){
                if(i==j)continue;
                else{
                    if(isCross(segments[i],segments[j])){
                        if(i < j){//如果和之后落下的相交,那么就被排除
                            isColl = false;
                        }break;
                    }
                }
            }
            if(isColl)
            {
                ans[ansNum++] = i + 1;
            }
        }
        cout<<"Top sticks: ";
        for(int k = 0; k < ansNum;k++){
            if(k < ansNum - 1)
                cout<<ans[k]<<", ";
            else
                cout<<ans[k]<<"."<<endl;
        }
    }
    bool Isolated::isCross(line la,line lb){
        long long dir1 = dir(la.st,la.en,lb.st);
        long long dir2 = dir(la.st,la.en,lb.en);
        long long dir3 = dir(lb.st,lb.en,la.st);
        long long dir4 = dir(lb.st,lb.en,la.en);
        if(dir1 * dir2 < 0 && dir3*dir4 < 0){// 线段 相交
            return true;
        }
        else if(dir1 == 0&& isInSeg(la.st,la.en,lb.st)){//向量共线,判断是否重合
            return true;
        }
        else if(dir2 == 0&&isInSeg(la.st,la.en,lb.en)){
            return true;
        }
        else if(dir3 == 0&&isInSeg(lb.st,lb.en,la.st)){
            return true;
        }
        else if(dir4 == 0&&isInSeg(lb.st,lb.en,la.en)){
            return true;
            //return false;
        }
        else
            return false;
    }
    bool Isolated::isInSeg(point la,point lb,point a){
        if((min(la.x,lb.x) <= a.x && a.x <= max(la.x,lb.x)) &&
           (min(la.y,lb.y) <= a.y && a.y <= max(la.y,lb.y)))
            return true;
        else
            return false;
    }
    long long Isolated::dir(point i,point j,point k){//向量叉积
        return ((j.x - i.x)*(k.y - i.y)-(k.x - i.x)*(j.y - i.y));
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
            freopen("D:\acm.txt","r",stdin);
        #endif // ONLINE_JUDGE
        int cases;
    
        Isolated isolated;
        while(cin>>cases,cases != 0){
            isolated.init();
            isolated.readData(cases);
            isolated.process();
        }
        return 0;
    }
    Donghua University
  • 相关阅读:
    【Ruby on Rails 学习五】Ruby语言的方法
    【Ruby on Rails 学习六】Ruby 类 的入门
    【Ruby on Rails 学习三】Ruby 基本数据类型(类、类的实例、对象)
    【深度学习笔记】第 1 课:从机器学习到深度学习
    【Ruby on Rails学习二】在线学习资料的整理
    【Ruby on Rails 学习一】ubuntu14.04配置rvm与ruby
    THREE.JS + Blender(obj、mtl加载代码)
    全站查询和分页显示——在线留言板2
    增加留言版块——在线留言板1
    html加载速度技术点
  • 原文地址:https://www.cnblogs.com/ohxiaobai/p/4526132.html
Copyright © 2020-2023  润新知