UVa 11343

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;
struct point {
    int x,y;
    point (int x = 0 ,int 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 result;
    public:
        void init();
        void readData();
        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(){
    result = 0;
    segments.clear();
}
void Isolated::readData(){
    cin>>segNum;
    int x1,y1,x2,y2;
    for(int i = 0;i < segNum;i++){
        cin>>x1>>y1>>x2>>y2;
        point st(x1,y1),en(x2,y2);
        segments.push_back(line(st,en));
    }
}
void Isolated::process(){
    bool isColl;//线段是否是孤立的
    for(int i = 0;i < segNum;i++){
        isColl = true;
        for(int j = 0;j < segNum;j++){
            if(i==j)continue;
            else{
                line a = segments[i] ,b = segments[j];
                if(isCross(a,b)){
                    isColl = false;
                    break;
                }
            }
        }
        if(isColl)
            result++;
    }
    cout<<result<<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;
    cin>>cases;
    Isolated isolated;
    while(cases--){
        isolated.init();
        isolated.readData();
        isolated.process();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/ohxiaobai/p/4525179.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值