POJ1127 Jack Straws(判断线段相交+Floyd-Warshall求传递闭包)

题目链接

Jack Straws

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

桌子上放着n根木棍,木棍i的两端坐标分别是(x1,y1)和(x2,y2)。给定m对木棍(ai,bi),判断每对木棍是否相连。当两根木棍间有公共点时,就认为它们是相连的(直接相连),通过相连的木棍间接的连在一起的两根木棍也认为是相连的(间接相连)。

判断两线段是否相交时,可利用快速排斥实验和跨立实验判断

判断间接相连时,可利用并查集也可利用Floyd-Warshall算法求传递闭包

AC代码:

//CSDN博客:https://blog.csdn.net/qq_40889820
#include<iostream>
#include<complex> 
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
struct Point
{
	double x,y;
};
double crossProduct(Point p1,Point q1,Point q2)//向量q1-p1和向量q1-q2的叉积
{
	double x1=p1.x-q1.x,y1=p1.y-q1.y,x2=q2.x-q1.x,y2=q2.y-q1.y;
	return x1*y2-y1*x2;
} 
bool isIntersect(Point p1,Point p2,Point q1,Point q2)//线段p1-p2和线段q1-q2是否相交 
{
	//快速排斥实验
	if(!((min(p1.x,p2.x)<=max(q1.x,q2.x))&&(min(q1.x,q2.x)<=max(p1.x,p2.x))&&
	   (min(p1.y,p2.y)<=max(q1.y,q2.y))&&(min(q1.y,q2.y)<=max(p1.y,p2.y))))
		return false;
	//跨立实验
	if((crossProduct(p1,q1,q2)*crossProduct(p2,q1,q2)>0)||
	   (crossProduct(q1,p1,p2)*crossProduct(q2,p1,p2)>0)) 
		return false;
	return true;
}
int main()
{
	int n,a,b;
	Point p[14],q[14];
	int g[14][14];//g[i][j]表示第i条木棍和第j条木棍直接或间接相连 
	while(cin>>n)
	{
		if(!n) break;
		for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y>>q[i].x>>q[i].y;
		mem(g,0);
		//直接相连 
		for(int i=1;i<=n;i++)
		{
			g[i][i]=true;
			for(int j=1;j<i;j++)
			if(isIntersect(p[i],q[i],p[j],q[j])) g[i][j]=g[j][i]=1;
		}
		//间接相连 ,利用Floyd-Warshall算法求传递闭包 
		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];		
		 
		while(cin>>a>>b)
		{
			if(!a&&!b) break;
			if(g[a][b]) cout<<"CONNECTED\n";
			else cout<<"NOT CONNECTED\n";
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40889820/article/details/86658210
今日推荐