POJ 1127 Jack Straws

Jack Straws

Time Limit: 1000MS Memory Limit: 10000K

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条线段,判断线段是否相交,间接相交也算相交

思路:

基本的步骤已在代码中表明了,如何用叉积求出两个线段的交点,这个链接里讲的挺清楚的。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps = 1e-10;
const int maxn = 15;
//考虑误差的加法
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);
    }
};
P p[maxn], q[maxn]; //一条线段两个点的坐标表示
//判断点q是否在线段p1-p2上
bool on_seg(P p1, P p2, P q) {
    return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}
//求出两个线段的交点,如何求在思路中有链接
P intersection(P p1, P p2, P q1, P q2) {
    return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
int main() {
    int n;
    while (scanf("%d", &n) != EOF && n) {
        bool g[maxn][maxn] = {false}; //判断两个点是否连接的数组
        for (int i = 0; i < n; i++) {
            scanf("%lf %lf %lf %lf", &p[i].x, &p[i].y, &q[i].x, &q[i].y);
        }
        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) {
                    //两直线叉积为0,代表平行
                    g[i][j] = g[j][i] = on_seg(p[i], q[i], p[j])
                                     || on_seg(p[i], q[i], q[j])
                                     || on_seg(p[j], q[j], p[i])
                                     || on_seg(p[j], q[j], q[i]); //如果有一个点在一条线段中间那么肯定连接
                } else {
                    //不平行时
                    P r = intersection(p[i], q[i], p[j], q[j]); //求出点的坐标位置
                    g[i][j] = g[j][i] = on_seg(p[i], q[i], r) && on_seg(p[j], q[j], r); //如果这个点符合在在两个线段的中间,那么这两个一定相交
                }
            }
        }
        //应用floyd算法,将间接相连的全部当作连接的
        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]);
                }
            }
        }
        int a, b;
        while (scanf("%d %d", &a, &b) != EOF && a + b) {
            if (g[a - 1][b - 1]) printf("CONNECTED\n"); //因为之前运算的时候都是从下标0开始运算的,所以a,b都要-1.
            else printf("NOT CONNECTED\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值