poj 1127 简单几何+并查集

来源:互联网 发布:cpu淘宝 编辑:程序博客网 时间:2024/05/07 12:56
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
71 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 020 2 0 00 0 0 11 12 21 20 00
Sample Output
CONNECTED NOT CONNECTED CONNECTED CONNECTED NOT CONNECTED CONNECTEDCONNECTEDCONNECTED

CONNECTED

题意:

在桌子上给出一些线段,然后问你两条线段是否相交,有两种情况

(1)直接相交

(2)a,b相交,b,c相交,如果a,c不直接相交也算相交

思路:

首先一看是并查集的问题,然后需要判断两天线是否有一个祖节点,即判断两条直线是否相交

(1).快速排斥试验设以线段 P1P2 为对角线的矩形为R, 设以线段 Q1Q2 为对角线的矩形为T,如果R和T不相交,显然两线段不会相交;(2).跨立试验如果两线段相交,则两线段必然相互跨立对方,P1P2跨立Q1Q2 ,则矢量 ( P1 - Q1 ) 和( P2 - Q1 )位于矢量( Q2 - Q1 ) 的两侧,即( P1 - Q1 ) × ( Q2 - Q1 ) * ( P2 - Q1 ) × ( Q2 - Q1 ) < 0上式可改写成( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) > 0当( P1 - Q1 ) × ( Q2 - Q1 ) = 0 时,说明( P1 - Q1 ) 和 ( Q2 - Q1 )共线,但是因为已经通过快速排斥试验,所以 P1 一定在线段 Q1Q2上;同理,( Q2 - Q1 ) ×(P2 - Q1 ) = 0 说明 P2 一定在线段 Q1Q2上。所以判断P1P2跨立Q1Q2的依据是:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) ≥ 0同理判断Q1Q2跨立P1P2的依据是:( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) ≥ 0
ac:代码
#include <iostream>
#include <cstdio>
using namespace std;
int pri[20];
struct point
{
  int x;
  int y;
};
struct line
{
    point a;
    point b;
}data[20];
int panduan(point a,point b,point c,point d)
{
  if(max(a.x,b.x)<min(c.x,d.x)||max(a.y,b.y)<min(c.y,d.y)||max(c.x,d.x)<min(a.x,b.x)||max(c.y,d.y)<min(a.y,b.y))
  return 0;
  int h,i,j,k;
  h = (a.x-c.x) * (d.y-c.y) - (a.y-c.y) * (d.x-c.x);
  i = (d.x-c.x) * (b.y-c.y) - (d.y-c.y) * (b.x-c.x);
  j = (c.x-a.x) * (b.y-a.y) - (c.y-a.y) * (b.x-a.x);
  k = (b.x-a.x) * (d.y-a.y) - (b.y-a.y) * (d.x-a.x);
  if(h*i>=0&&j*k>=0)
  return 1;
  else
  return 0;
}
int find(int x)
{
   if(x==pri[x])
   return pri[x];
   else
   return pri[x]=find(pri[x]);
}
int main()
{
    int n;
    while(cin>>n&&n)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&data[i].a.x,&data[i].a.y,&data[i].b.x,&data[i].b.y);
        }
        for(int i=1;i<=n;i++)
        pri[i]=i;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if(panduan(data[i].a,data[i].b,data[j].a,data[j].b))
                {
                    int x=find(i);
                    int y=find(j);
                    if(x!=y)
                    pri[x]=y;
                }
            }
        }
        for(int i=1;i<=n;i++)
        pri[i]=find(i);
        int x1,y1;
        while(~scanf("%d%d",&x1,&y1))
        {
          if(x1==0&&y1==0)
          break;
          if(pri[x1]==pri[y1])
          printf("CONNECTED\n");
          else
          printf("NOT CONNECTED\n");
        }
    }
    return 0;
}

0 0
原创粉丝点击