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 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.
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.
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
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED
水题,线段交+并查集:
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=15;
int father[maxn],n;
struct POINT{
double x,y;
POINT(double a=0,double b=0) {x=a; y=b;}
};
struct segment{
POINT s,e;
segment(POINT a,POINT b) {s=a;e=b;}
segment() {}
}s[maxn];
//叉积
double cross(POINT a,POINT b,POINT c){
return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
//判断点在线段上
bool online(segment l,POINT p){
return ((cross(l.e,p,l.s)==0) && (((p.x-l.s.x)*(p.x-l.e.x)<=0) && ((p.y-l.s.y)*(p.y-l.e.y)<=0)));
}
// 如果线段u和v相交(包括相交在端点处)时,返回true
bool intersect(segment u,segment v){
return ((max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&& //排斥实验
(max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
(max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
(max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
(cross(v.s,u.e,u.s)*cross(u.e,v.e,u.s)>=0)&& //跨立实验
(cross(u.s,v.e,v.s)*cross(v.e,u.e,v.s)>=0));
}
int Findfather(int x)
{
if (x != father[x])
father[x] = Findfather(father[x]);
return father[x];
}
void Union(int x,int y)
{
int m=Findfather(x);
int n=Findfather(y);
if(m!=n){
father[n]=m;
}
}
void init(){
for(int i=0;i<=n;i++) father[i]=i;
}
int main()
{
while(cin>>n && n){
init();
for(int i=1;i<=n;i++){
cin>>s[i].s.x>>s[i].s.y>>s[i].e.x>>s[i].e.y;
}
int x,y;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(intersect(s[i],s[j])) Union(i,j);
}
}
while(cin>>x>>y && (x && y)){
if(Findfather(x)==Findfather(y)) puts("CONNECTED");
else puts("NOT CONNECTED");
}
}
return 0;
}