# POJ 1127 Jack Straws C语言版

POJ题解 2190浏览

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

Slyar:说下题目大意。首先给出一个n，表示共有n条边，下面n行(编号1-n)每行给出4个数分别表示起始点的横纵坐标以及终止点的横纵坐标。接下来的若干行为查询条件，要你输出查询的两条边是否有关系(即直接相交或间接相交)。

```#include
#include

#define MAX 15

/* 定义点信息，x为横坐标，y为纵坐标 */
typedef struct
{
double x;
double y;
}point;

/* 定义边信息，s为起始节点，e为终止节点 */
typedef struct
{
point s;
point e;
}line;

line p[MAX];
int father[MAX];
int rank[MAX];

/* 初始化集合 */
void Make_Set(int x)
{
father[x] = x;
rank[x] = 0;
}

/* 查找x元素所在的集合,回溯时压缩路径 */
int Find_Set(int x)
{
if (x != father[x])
{
father[x] = Find_Set(father[x]);
}
return father[x];
}

/* 合并x,y所在的集合 */
void Union(int x, int y)
{
if (x == y) return;
if (rank[x] > rank[y])
{
father[y] = x;
}
else
{
if (rank[x] == rank[y])
{
rank[y]++;
}
father[x] = y;
}
}

/* 最大值函数 */
double max(double x, double y)
{
return x > y ? x : y;
}

/* 最小值函数 */
double min(double x, double y)
{
return x < y ? x : y;
}

/* 叉积 (a-c)x(b-c) */
double multiply(point a, point b, point c)
{
return (a.x - c.x) * (b.y - c.y) - (b.x - c.x) * (a.y - c.y);
}

/* 跨立判断 */
int intersect(line u, line 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))
&& (multiply(v.s, u.e, u.s) * multiply(u.e, v.e, u.s) >= 0)
&& (multiply(u.s, v.e, v.s) * multiply(v.e, u.e, v.s) >= 0));
}

int main()
{
int n, i, j;
int a, b, x, y;
while(1)
{
scanf("%d", &n);
if (n == 0) break;
/* 初始化集合 */
for(i = 1; i <= n; i++)
{
Make_Set(i);
}
/* 读入边信息 */
for(i = 1; i <= n; i++)
{
scanf("%lf%lf%lf%lf", &p[i].s.x, &p[i].s.y, &p[i].e.x, &p[i].e.y);
}
/* 处理每条边，如果相交就合并 */
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
if(i != j && intersect(p[i], p[j]))
{
x = Find_Set(i);
y = Find_Set(j);
Union(x,y);
}
}
}
/* 进行查询 */
while(1)
{
scanf("%d %d", &a, &b);
if (a + b == 0) break;
/* 自己和自己肯定有关系 */
if(a == b)
{
printf("CONNECTED\n");
}
else
{
/* 若在一个集合则两边有关系 */
x = Find_Set(a);
y = Find_Set(b);
if(x == y)
{
printf("CONNECTED\n");
}
else
{
printf("NOT CONNECTED\n");
}
}
}
}
//system("pause");
return 0;
}
```