题目大意:求出孤立线段数目(孤立线段:与其他线段不相交的线段)。
解题策略:还是向量法解决,但是因为溢出的问题,wa了一宿,一度以为算法不严密,大家注意溢出!
/*
UVA 11343 Isolated Segments
AC by J_Dark
ON 2013/5/2 17:09
Time 0.015s
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#include <algorithm>
using namespace std;
//int wa, double wa, long long int AC!!!!!!!!!尼玛呀!!!昨晚纠结到半夜,还以为算法问题……
typedef long long int LL;
struct point{
LL x, y;
point(LL p=0, LL q=0){
x = p;
y = q;
}
};
struct line{
point a, b;
line(point x, point y){
a = x;
b = y;
}
};
vector<line> Seg;
int segNum;
//
void Input(){
Seg.clear();
LL ax, ay, bx, by;
cin >> segNum;
for(int i=0; i<segNum; i++){
cin >> ax >> ay >> bx >> by;
point start(ax, ay), end(bx, by);
Seg.push_back(line(start, end));
}
}
int Direction(point Pi, point Pj, point Pk){
return (Pj.x-Pi.x)*(Pk.y-Pi.y)-(Pk.x-Pi.x)*(Pj.y-Pi.y);
//return (Pk.x-Pi.x)*(Pj.y-Pi.y) - (Pj.x-Pi.x)*(Pk.y-Pi.y);
}
//判断pk点是否在线段pi-pj上
bool OnSegment(point Pi, point Pj, point Pk){
if(min(Pi.x, Pj.x) <= Pk.x && max(Pi.x, Pj.x) >= Pk.x &&
min(Pi.y, Pj.y) <= Pk.y && max(Pi.y, Pj.y) >= Pk.y)
return true;
return false;
}
//判断线段p,q是否相交
bool isIntersect(line p, line q){
LL d1, d2, d3, d4;
d1 = Direction(p.a, p.b, q.a);
d2 = Direction(p.a, p.b, q.b);
d3 = Direction(q.a, q.b, p.a);
d4 = Direction(q.a, q.b, p.b);
if(d1*d2<0 && d3*d4<0) { return true;} //规范相交
//非规范相交
else if(d1==0 && OnSegment(p.a, p.b, q.a)) {return true;}
else if(d2==0 && OnSegment(p.a, p.b, q.b)) {return true;}
else if(d3==0 && OnSegment(q.a, q.b, p.a)) {return true;}
else if(d4==0 && OnSegment(q.a, q.b, p.b)) {return true;}
else return false;
}
void Compute(){
int ansNum = 0, ff;
for(int i=0; i<segNum; i++){
ff = 0;
for(int k=0; k<segNum; k++){
if(i != k){
if(isIntersect(Seg[i], Seg[k])){
ff = 1;
break;
}
}
}
if(!ff)
ansNum++;
}
cout << ansNum << endl;
}
//
int main(){
int testCase;
while(cin >> testCase)
{
while(testCase--)
{
Input();
Compute();
//Output();
}
}
return 0;
}