Jack Straws
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5518 | Accepted: 2516 |
Description
In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws.
Input
Input consist multiple case,each case consists of multiple lines. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers,x1,y1,x2 and y2, giving the coordinates, (x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of the current case(except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the current case is terminated.
When n=0,the input is terminated.
There will be no illegal input and there are no zero-length straws.
Output
You should generate a line of output for each line containing a pair a and b, except the final line where a = 0 = b. The line should say simply "CONNECTED", if straw a is connected to straw b, or "NOT CONNECTED", if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself.
Sample Input
7
1 6 3 3
4 6 4 9
4 5 6 7
1 4 3 5
3 5 5 5
5 2 6 3
5 4 7 2
1 4
1 6
3 3
6 7
2 3
1 3
0 0
2
0 2 0 0
0 0 0 1
1 1
2 2
1 2
0 0
0
Sample Output
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED
内积:
外积/叉积:
小性质:
1 x
<0这表明
在
的顺时针方向
2 x
>0这表明
在
的逆时针方向
3 x
=0这表明
平行于
(如果他们的内积是<=0 则表明他们共线)
代数规则:
1、反交换律:a×b=-b×a
2、加法的分配律:a×(b+c)=a×b+a×c。
3、与标量乘法兼容:(ra)×b=a×(rb)=r(a×b)。
4、不满足结合律,但满足雅可比恒等式:a×(b×c)+b×(c×a)+c×(a×b)=0。
5、分配律,线性性和雅可比恒等式别表明:具有向量加法和叉积的R3构成了一个李代数。
6、两个非零向量a和b平行,当且仅当a×b=0。 [1]
判断是否平行
直接利用小性质3
利用叉积判断两直线的交点
直线ab: a =(x1,y1) b=(x2,y2) 则:ab=(x2-x1,y2-y1)
直线cd: c=(x3,y3) d=(x4,y4) 则:cd=(x4-x3,y4-y3)
设一个变量 t,则直线 ab 上的点可以表示成 a + t (b-a) 。又该点也在直线 cd 上,所以有 (d- c) X (a + t(b- a) - c) = 0 。解出 t 的表达式后代入a + t (b-a) 即得到了交点的向量即坐标:
a + (b - a) * ((d - c).det(c - a) / (d - c).det(b - a));
摸版:
//判断点p是否在线段p1,p2上
int on_seg(Point p1,Point p2,Point p)
{
return (p1-p).det(p2-p)==0&&(p1-p).dot(p2-p)<=0;
}
//求两条直线的交点
Point inter(Point a,Point b,Point c,Point d)
{
return a + (b - a) * ((d - c).det(c - a) / (d - c).det(b - a));
}
利用叉积判断两直线是否相交
利用叉积的性质1,2判断其中一条直线的两个端点是否在另一条直线的两边及叉积的成绩小于0,如果有端点与直线共线判断端点是否在直线上;
摸版:
//判断点是否在直线上
int on_seg(Point p1,Point p2,Point p)
{
return (p1-p).det(p2-p)==0&&(p1-p).dot(p2-p)<=0;
}
int cmp(Seg a,Seg b)
{
double d1=(a.b-a.a).det(b.a-a.a),d2=(a.b-a.a).det(b.b-a.a);
double d3=(b.b-b.a).det(a.a-b.a),d4=(b.b-b.a).det(a.b-b.a);
if(d1*d2<0&&d3*d4<0){return 1;}
if(d1==0&&on_seg(a.b,a.a,b.a)){return 1;}
if(d2==0&&on_seg(a.b,a.a,b.b)){return 1;}
if(d3==0&&on_seg(b.a,b.b,a.a)){return 1;}
if(d4==0&&on_seg(b.a,b.b,a.b)){return 1;}
return 0;
}
做法一用利用Floyd算法+直线交点
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
const int maxn=1e5+5;
const int minn=100;
const double eps=1e-10;
double add(double a,double b)
{
if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
return a+b;
}
struct Point{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
Point operator +(Point p)
{
return Point(add(x,p.x),add(y,p.y));
}
Point operator -(Point p)
{
return Point(add(x,-p.x),add(y,-p.y));
}
Point operator *(double d)
{
return Point(x*d,y*d);
}
double dot(Point p)//内积
{
return add(x*p.x,y*p.y);
}
double det(Point p)//外积
{
return add(x*p.y,-p.x*y);
}
};
//判断点p是否在线段p1,p2上
int on_seg(Point p1,Point p2,Point p)
{
return (p1-p).det(p2-p)==0&&(p1-p).dot(p2-p)<=0;
}
//求两条直线的交点
Point inter(Point a,Point b,Point c,Point d)
{
return a + (b - a) * ((d - c).det(c - a) / (d - c).det(b - a));
}
int n,m;
Point p[minn],q[minn];
int g[minn][minn];
void slove()
{
clr(g,0);
for(int i=1;i<=n;i++)
{
g[i][i]=1;
for(int j=1;j<=n;j++)
{
if((p[i]-q[i]).det(p[j]-q[j])==0)//两个线段平行
{
g[i][j]=g[j][i]=on_seg(p[i],q[i],p[j])||on_seg(p[i],q[i],q[j])
||on_seg(p[j],q[j],p[i])||on_seg(p[j],q[j],q[i]);//判断是否重合
}
else
{
Point tm=inter(p[i],q[i],p[j],q[j]);
g[i][j]=g[j][i]=on_seg(p[i],q[i],tm)&&on_seg(p[j],q[j],tm);
}
}
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
g[i][j]|=g[i][k]&&g[k][j];
}
}
}
return ;
}
int main()
{
#ifndef ONLIE_JUDGE
freopen("data.txt","r",stdin);
#endif // ONLIE_JUDGE
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&q[i].x,&q[i].y);
}
slove();
int x,y;
while(~scanf("%d%d",&x,&y)&&x&&y)
{
puts(g[x][y]?"CONNECTED":"NOT CONNECTED");
}
}
return 0;
}
做法二:利用线段相交+并查集
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
const int maxn=1e5+5;
const int minn=100;
const double eps=1e-10;
double add(double a,double b)
{
if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
return a+b;
}
struct Point{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
Point operator +(Point p)
{
return Point(add(x,p.x),add(y,p.y));
}
Point operator -(Point p)
{
return Point(add(x,-p.x),add(y,-p.y));
}
Point operator *(double d)
{
return Point(x*d,y*d);
}
double dot(Point p)//内积
{
return add(x*p.x,y*p.y);
}
double det(Point p)//外积
{
return add(x*p.y,-p.x*y);
}
};
struct Seg{
Point a,b;
}se[maxn];
int n,m,f[minn];
int findt(int x)
{
return x==f[x]?x:f[x]=findt(f[x]);
}
//判断点是否在直线上
int on_seg(Point p1,Point p2,Point p)
{
return (p1-p).det(p2-p)==0&&(p1-p).dot(p2-p)<=0;
}
int cmp(Seg a,Seg b)
{
double d1=(a.b-a.a).det(b.a-a.a),d2=(a.b-a.a).det(b.b-a.a);
double d3=(b.b-b.a).det(a.a-b.a),d4=(b.b-b.a).det(a.b-b.a);
if(d1*d2<0&&d3*d4<0){return 1;}
if(d1==0&&on_seg(a.b,a.a,b.a)){return 1;}
if(d2==0&&on_seg(a.b,a.a,b.b)){return 1;}
if(d3==0&&on_seg(b.a,b.b,a.a)){return 1;}
if(d4==0&&on_seg(b.a,b.b,a.b)){return 1;}
return 0;
}
//判断两直线是否相交
void slove()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(cmp(se[i],se[j]))
{
int fj=findt(j);
int fi=findt(i);
f[fj]=fi;
}
}
}
}
int main()
{
/*#ifndef ONLIE_JUDGE
freopen("data.txt","r",stdin);
#endif*/
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
{
f[i]=i;
scanf("%lf%lf%lf%lf",&se[i].a.x,&se[i].a.y,&se[i].b.x,&se[i].b.y);
}
slove();
int x,y;
while(~scanf("%d%d",&x,&y)&&x&&y)
{
puts(findt(x)==findt(y)?"CONNECTED":"NOT CONNECTED");
}
}
return 0;
}