TOJ 1840 POJ 1127 HDU 1102 Jack Straws / 线段相交 + 并查集

Jack Straws

时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte

描述

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

 

题意就是判断是否2条线段相交 这个相交可以是间接相交 就是1和2 2 和3 分别相交 但是1和3虽然没有直接相交 但是也算 用并查集合并到一个集合就行 看看他们是否有公共的祖先 判断线段相交用叉积

#include <stdio.h>
#include <math.h>
struct point
{
	double x,y;
};
struct node
{
	point p1,p2;
}a[20];

int bin[20];
double min(double x,double y)
{
	return x<y?x:y;
}
double max(double x,double y)
{
	return x>y?x:y;
}
void init()
{
	int i;
	for(i = 1;i <= 18; i++)
		bin[i] = i;
}

int find(int x)
{
	if(x!=bin[x])
		return bin[x] = find(bin[x]);
	return bin[x];
	
}
double CHAJI(point a,point b,point c)
{
	double p = (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
	if(p>0)
		return 1;
	if(p<0)
		return -1;
	if(p==0)
		return 0;
}
bool PAICHI(point a,point b,point c)
{
	if(a.x>=min(b.x,c.x)&&a.x<=max(b.x,c.x)&&a.y>=min(b.y,c.y)&&a.y<=max(b.y,c.y))
		return true;
	return false ;
}
bool KUALI(int i,int j)
{
	double d1 = CHAJI(a[i].p1,a[j].p1,a[j].p2);
	double d2 = CHAJI(a[i].p2,a[j].p1,a[j].p2);
	double d3 = CHAJI(a[j].p1,a[i].p1,a[i].p2);
	double d4 = CHAJI(a[j].p2,a[i].p1,a[i].p2);
	if(d1*d2<0&&d3*d4<0)
		return true;
	if(d1==0&&d2==0&&d3==0&&d4==0)
		return true;
	if(d1==0&&PAICHI(a[i].p1,a[j].p1,a[j].p2))
		return true;
	if(d2==0&&PAICHI(a[i].p2,a[j].p1,a[j].p2))
		return true;
	if(d3==0&&PAICHI(a[j].p1,a[i].p1,a[i].p2))
		return true;
	if(d4==0&&PAICHI(a[j].p2,a[i].p1,a[i].p2))
		return true;
	return false;
}
void merge(int x,int y)
{
	if(KUALI(x,y))
	{
		x = find(x);
		y = find(y);
		if(x>y)
			bin[x] = y;
		else
			bin[y] = x;
	}
}


main()
{
	int t,i,j,x,y;
	while(scanf("%d",&t),t)
	{
		init();
		for(i = 1;i <= t; i++)
		{
			scanf("%lf %lf %lf %lf",&a[i].p1.x,&a[i].p1.y,&a[i].p2.x,&a[i].p2.y);
			for(j = 1; j < i; j++)
			{
				merge(i,j);
			}
		}
		while(scanf("%d %d",&x,&y),x||y)
		{
			if(find(x)==find(y))
				printf("CONNECTED\n");
			else	
			printf("NOT CONNECTED\n");
		}
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值