POJ 1127(Jack Straws )(线段相交)

来源:互联网 发布:职位表数据库字段 编辑:程序博客网 时间:2024/05/10 16:55

原题链接

Problem 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根木棍的坐标,有m个询问(ai,bi),要求判断第ai根木棍是否与第bi根木根相连,通过若干根木棍间接相连也被认为是相连。

解题思路

计算几何初步,运用到向量的点积和叉积来判断点是否在线段上以及求出两条线段及其延长线的交点。

AC代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<string>#include<queue>#include<list>#include<stack>#include<set>#include<map>#define ll long long#define ull unsigned long long#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define rrep(i,a,b) for(int i=(a),_ed=(b);i>=_ed;i--)#define fil(a,b) memset((a),(b),sizeof(a))#define cl(a) fil(a,0)#define pb push_back#define PI 3.1415927#define inf 0x3f3f3f3f#define maxn 15#define maxm 10005using namespace std;int n;double eps=1e-10;double add(double a,double b){    if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;    else return a+b;}struct P{    double x,y;    P() {}    P(double x , double y) :x(x),y(y) {}    P operator + (P p){return P(add(x,p.x),add(y,p.y));}    P operator - (P p) {return P(add(x,-p.x),add(y,-p.y));}    P operator * (double d) {return P(x*d,y*d);}    double dot(P p) {return add(x*p.x,y*p.y);}    double det(P p) {return add(x*p.y,-y*p.x);}};bool onseg(P p1,P p2,P q) {return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;}P inter(P p1,P p2,P q1,P q2) {return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));}int m;int main(){    while(cin>>n&&n!=0)    {        int a[maxm];        int b[maxm];        bool g[maxn][maxn];        for(int i=0;i<maxn;++i)        {            for(int j=0;j<maxn;++j) g[i][j]=false;        }        P p[maxn];        P q[maxn];        double t1,t2,t3,t4;        int ta,tb;        m=0;        rep(i,0,n-1)        {            scanf("%lf%lf%lf%lf",&t1,&t2,&t3,&t4);            p[i].x=t1;            p[i].y=t2;            q[i].x=t3;            q[i].y=t4;        }        while(scanf("%d%d",&ta,&tb)&&(ta||tb))        {            a[m++]=ta;            b[m-1]=tb;        }        for(int i=0;i<n;++i)        {            g[i][i]=true;            for(int j=0;j<i;++j)            {                if((p[i]-q[i]).det(p[j]-q[j])==0)                {                    g[i][j]=g[j][i]=(onseg(p[i],q[i],q[j])||                                     onseg(p[i],q[i],p[j])||                                     onseg(p[j],q[j],p[i])||                                     onseg(p[j],q[j],q[i]));                }                else                {                    P r=inter(p[i],q[i],p[j],q[j]);                    g[i][j]=g[j][i]=onseg(p[i],q[i],r)&&onseg(p[j],q[j],r);                }            }        }        for(int k=0;k<n;++k)        {            for(int i=0;i<n;++i)            {                for(int j=0;j<n;++j)                {                    g[i][j]|=g[i][k]&&g[k][j];                }            }        }        for(int i=0;i<m;++i)        {            if(g[a[i]-1][b[i]-1]) printf("CONNECTED\n");            else printf("NOT CONNECTED\n");        }    }    return 0;}
0 0
原创粉丝点击