标签:计算几何
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 3155 | Accepted: 1418 |
Description
Input
Output
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
Source
#include <iostream>
#include <cstdio>
using namespace std;
int set[20];
struct Point
{
int x1,x2,y1,y2;
Point(int x1 = 0, int x2 = 0, int y1 = 0, int y2 = 0) : x1(x1),x2(x2),y1(y1),y2(y2) {};
void read()
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
}
}p[20];
void unit(int n)
{
for(int i = 1; i <= n; i++) set[i] = i;
}
int find(int x)
{
return x == set[x] ? x : set[x] = find(set[x]);
}
int cross(int x1, int y1, int x2, int y2)
{
return x1 * y2 - x2 * y1;
}
int intersection(Point A, Point B) //判断直线相交
{
int c[4];
if(max(A.x1,A.x2) < min(B.x1,B.x2) || max(A.y1,A.y2) < min(B.y1,B.y2)
|| max(B.x1,B.x2) < min(A.x1,A.x2) || max(B.y1,B.y2) < min(A.y1,A.y2) ) return 0; //考虑共线不相交的情况,为快速排斥定理
/*判断两条直线是否相交,即只需判断线是否在另一条线的两端*/
c[0] = cross(A.x2 - A.x1, A.y2 - A.y1, B.x1 - A.x1, B.y1 - A.y1);
c[1] = cross(A.x2 - A.x1, A.y2 - A.y1, B.x2 - A.x1, B.y2 - A.y1);
c[2] = cross(B.x2 - B.x1, B.y2 - B.y1, A.x1 - B.x1, A.y1 - B.y1);
c[3] = cross(B.x2 - B.x1, B.y2 - B.y1, A.x2 - B.x1, A.y2 - B.y1);
if(c[0] * c[1] <= 0 && c[2] * c[3] <= 0) return 1; //运用到了向量的叉乘和点乘的知识;
return 0;
}
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n) && n)
{
for(int i = 1; i <= n; i++)
p[i].read();
unit(n);
for(int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
if(intersection(p[i],p[j]))
{
int a = find(i);
int b = find(j);
if(a != b) set[a] = b;
}
}
}
int a,b;
while(~scanf("%d %d",&a,&b), a | b)
{
a = find(a);
b = find(b);
if(a == b) puts("CONNECTED");
else puts("NOT CONNECTED");
}
}
return 0;
}
标签:计算几何
原文地址:http://blog.csdn.net/zsgg_acm/article/details/39100789