思路:
直接判断两条线是否相交,然后用floyd判断连通性
c o d e code code
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n;
bool dis[110][110];
struct abc
{
int x, y;
};
struct node{
abc p1, p2;
}p[10010];
double js(abc p1, abc p2, abc p3)
{
return (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x);
}
bool online(abc p1, abc p2, abc p3)
{
if(p3.x>=min(p1.x, p2.x)&&p3.x<=max(p1.x, p2.x)&&
p3.y>=min(p1.y, p2.y)&&p3.y<=max(p1.y, p2.y))
return 1;
else return 0;
}
bool check(abc p1, abc p2, abc p3, abc p4)
{
if(js(p3, p4, p1)*js(p3, p4, p2)<0&&js(p1, p2, p3)*js(p1, p2, p4)<0)
return 1;
else if(js(p3, p4, p1)==0&&online(p3, p4, p1))
return 1;
else if(js(p3, p4, p2)==0&&online(p3, p4, p2))
return 1;
else if(js(p1, p2, p3)==0&&online(p1, p2, p3))
return 1;
else if(js(p1, p2, p4)==0&&online(p1, p2, p4))
return 1;
return 0;
}
int main()
{
scanf("%d", &n);
while(n!=0)
{
for(int i=1; i<=n; i++)
scanf("%d%d%d%d", &p[i].p1.x, &p[i].p1.y, &p[i].p2.x, &p[i].p2.y);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(check(p[i].p1, p[i].p2, p[j].p1, p[j].p2))
dis[i][j]=1;
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dis[i][j]=dis[i][j]||(dis[i][k]&&dis[k][j]);
int xx, yy;
scanf("%d%d", &xx, &yy);
while(xx!=0&&yy!=0)
{
if(dis[xx][yy])
printf("CONNECTED\n");
else printf("NOT CONNECTED\n");
scanf("%d%d", &xx, &yy);
}
memset(dis, 0, sizeof(dis));
scanf("%d", &n);
}
return 0;
}