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;
}

 

posted @ 2015-11-20 18:16  小白v  阅读(302)  评论(0编辑  收藏  举报