计算几何--线段相交

swust oj 680

Jack Straws
1000(ms) 65535(kb) 450 / 1259
n 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.

输入
A problem consists of multiple lines of input. 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 input (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 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
样例输出
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED

一道基础的 计算几何+并查集

题意就是将所有相交的线段放到一个集合里面。。。。。。。

所以就是先判断是否相交再并查集。。。。。

判断直线是否相交一般使用 叉积

对于两个向量的叉积我们一般这样表示:x1y2 - x2y1

由叉积的意义我们可以知道:


叉积的正负与旋转的方向有关,比如ac转到cd 与 cb转到cd的正负不一样。。。。。所以我们就能判断是否相交,,,,但是在这之前必选先快速排斥一下,因为有可能刚好端点在另一条线上面。。。。

#include<bits/stdc++.h>
using namespace std;
const int N=20;
int f[N],n;
struct node
{
	int x,y;
}p[N],q[N];
int check(int i,int j)
{
	if(max(q[i].x,p[i].x)<min(q[j].x,p[j].x)||
	max(q[i].y,p[i].y)<min(q[j].y,p[j].y)||
	max(q[j].x,p[j].x)<min(q[i].x,p[i].x)||
	max(q[j].y,p[j].y)<min(q[i].y,p[i].y))	return 0;//快速排斥
	
	int u=(p[j].x-p[i].x)*(q[i].y-p[i].y)-(q[i].x-p[i].x)*(p[j].y-p[i].y);
	int v=(q[j].x-p[i].x)*(q[i].y-p[i].y)-(q[i].x-p[i].x)*(q[j].y-p[i].y);
	int w=(q[i].x-p[j].x)*(q[j].y-p[j].y)-(q[j].x-p[j].x)*(q[i].y-p[j].y);
	int z=(p[i].x-p[j].x)*(q[j].y-p[j].y)-(q[j].x-p[j].x)*(p[i].y-p[j].y);
	return (u*v<=0&&w*z<=0);//跨立实验
}
int get(int x)
{
	return x==f[x]?x:f[x]=get(f[x]);
}
void union_set()
{
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)	if(check(i,j))	f[get(i)]=get(j);
	}
}
int main()
{
	while(cin>>n)
	{
		for(int i=1;i<=n;i++)	f[i]=i;
		for(int i=1;i<=n;i++)	cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y;
		union_set();
		int a,b;
		while(cin>>a>>b,a||b)
		{
			if(get(a)==get(b))	cout<<"CONNECTED"<<endl;
			else	cout<<"NOT CONNECTED"<<endl;
		}
	}
	return 0;
}
全部评论

相关推荐

11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务