您的位置:首页 > 其它

POJ 1127 Jack Straws(计算几何)

2013-10-11 16:41 344 查看
题目链接

抄的模版,居然1Y了。就是简单的线段相交+并查集。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define eps 1e-8
#define zero(x) (((x) > 0?(x):(-x))<eps)

struct point
{
double x,y;
};
struct line
{
point a,b;
};
int o[100];
line p[101];
int xmult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y) - (p1.y-p0.y)*(p2.x-p0.x);
}
int dot_online_in(point p,line l)
{
return zero(xmult(p,l.a,l.b))&&(l.a.x-p.x)*(l.b.x-p.x) < eps&&(l.a.y-p.y)*(l.b.y-p.y) < eps;
}
int dots_online(point p1,point p2,point p3)
{
return zero(xmult(p1,p2,p3));
}
int same_side(point p1,point p2,line l)
{
return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b) > eps;
}
int interset_in(line u,line v)
{
if(!dots_online(u.a,u.b,v.a)||!dots_online(u.a,u.b,v.b))
return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u);

return dot_online_in(u.a,v)||dot_online_in(u.b,v)||dot_online_in(v.a,u)||dot_online_in(v.b,u);
}
int find(int x)
{
while(x != o[x])
x = o[x];
return x;
}
void merge(int x,int y)
{
x = find(x);
y = find(y);
if(x != y)
o[x] = y;
}
int main()
{
int n,i,j,x,y;

while(scanf("%d",&n)!=EOF)
{
if(n == 0) break;
for(i = 1;i <= n;i ++)
{
o[i] = i;
scanf("%lf%lf%lf%lf",&p[i].a.x,&p[i].a.y,&p[i].b.x,&p[i].b.y);
}
for(i = 1;i <= n;i ++)
{
for(j = i+1;j <= n;j ++)
{
if(interset_in(p[i],p[j]))
merge(i,j);
}
}
for(;;)
{
scanf("%d%d",&x,&y);
if(x == 0&&y == 0) break;
if(find(x) == find(y))
printf("CONNECTED\n");
else
printf("NOT CONNECTED\n");
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: