• 原题如下:

    Jack Straws
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5555   Accepted: 2536

    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
  • 题解:问题的关键是判断线段是否相交,然后就可以建图进行连接性判断。首先会想到计算两直线的交点然后判断交点是否在线段上的方法,这样问题就变成如何判断点是否在线段上以及如何求两直线的交点。在几何问题中,运用向量的内积和外积进行计算是非常方便的。对于二维向量p1=(x1,y1)和p2=(x2,y2),定义内积p1·p2=x1x2+y1y2,外积p1×p2=x1y2-y1x2。要判断点q是否在线段p1-p2上,只要先根据外积(p1-q)×(p2-q)是否等于0来判断点q是否在直线p1-p2上,再利用内积(p1-q)·(p2-q)是否小于等于0来判断点q是否落在p1-p2之间。而要求两直线的交点,通过变量t将直线p1-p2上的点表示为p1+t(p2-p1),交点又在直线q1-q2上,所以有:(q2-q1)×(p1+t(p2-p1)-q1)=0,于是可以利用下式求得t的值:

    但是,使用这个方法还要注意边界情况,如果两条线段是平行的,也有可能有公共点,可以通过检查端点是否在另一条线段上来判断。
  • 代码:
     #include <cstdio>
    #include <cmath>
    #include <algorithm> using namespace std; const double EPS=1e-; double add(double a, double b)
    {
    if (fabs(a+b)<EPS*(fabs(a)+fabs(b))) return ;
    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 on_seg(P p1, P p2, P q)
    {
    return (p1-q).det(p2-q)== && (p1-q).dot(p2-q)<=;
    } P intersection(P p1, P p2, P q1, P q2)
    {
    return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
    } const int MAX_N=;
    const int MAX_M=;
    int n;
    P p[MAX_N], q[MAX_N];
    int m;
    int a[MAX_M], b[MAX_M];
    bool g[MAX_N][MAX_N]; int main()
    {
    while (~scanf("%d", &n) && n)
    {
    fill(g[], g[]+sizeof(bool)**, false);
    for (int i=; i<n; i++)
    {
    scanf("%lf %lf %lf %lf", &p[i].x, &p[i].y, &q[i].x, &q[i].y);
    }
    for (int i=;;i++)
    {
    scanf("%d %d",&a[i], &b[i]);
    if (a[i]== && b[i]==)
    {
    m=i;
    break;
    }
    }
    for (int i=; i<n; i++)
    {
    g[i][i]=true;
    for (int j=; j<i; j++)
    {
    if ((p[i]-q[i]).det(p[j]-q[j])==)
    {
    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);
    }
    }
    }
    for (int k=; k<n; k++)
    for (int i=; i<n; i++)
    for (int j=; j<n; j++)
    {
    g[i][j] |= g[i][k]&&g[k][j];
    }
    for (int i=; i<m; i++)
    {
    puts(g[a[i]-][b[i]-] ? "CONNECTED" : "NOT CONNECTED");
    }
    }
    }

Jack Straws(POJ 1127)的更多相关文章

  1. Jack Straws POJ - 1127 (简单几何计算 + 并查集)

    In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...

  2. Jack Straws POJ - 1127 (几何计算)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5428   Accepted: 2461 Descr ...

  3. Jack Straws(poj 1127) 两直线是否相交模板

    http://poj.org/problem?id=1127   Description In the game of Jack Straws, a number of plastic or wood ...

  4. poj 1127:Jack Straws(判断两线段相交 + 并查集)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2911   Accepted: 1322 Descr ...

  5. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  6. poj1127 Jack Straws(线段相交+并查集)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory L ...

  7. 1840: Jack Straws

    1840: Jack Straws 时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte 总提交: 168            测试通过:129 描述 I ...

  8. TZOJ 1840 Jack Straws(线段相交+并查集)

    描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...

  9. poj 1127(直线相交+并查集)

    Jack Straws Description In the game of Jack Straws, a number of plastic or wooden "straws" ...

随机推荐

  1. LeetCode 120. Triangle (三角形最小路径和)详解

    题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...

  2. Android 开发学习进程0.17 Android资源文件selector textview显示两种不同字体

    selector 是安卓资源文件的一种,它可以使按钮等实现不同状态下的不同UI,不用在代码中实现,而使用方式有两种,一种在color文件下 创建.xml可以使按钮等字体在不同状态下的变化,其二是在dr ...

  3. Linux与Windows的区别(后面了解后继续更新)

    1.转自知乎上的@Haoyuan Xing得答案: Linux是一个以开发者为中心的操作系统,Windows是以消费者为中心的操作系统.这是最根本的区别,也是Linux相对于Windows的优势/劣势 ...

  4. manjaro与python开发环境配置

    1.manjaro配置 1.1.启动项 sudo update-grub 注:Manjaro(archLinux)系统时间快8小时--> sudo timedatectl set-local-r ...

  5. 布隆过滤器(Bloom Filters)的原理及代码实现(Python + Java)

    本文介绍了布隆过滤器的概念及变体,这种描述非常适合代码模拟实现.重点在于标准布隆过滤器和计算布隆过滤器,其他的大都在此基础上优化.文末附上了标准布隆过滤器和计算布隆过滤器的代码实现(Java版和Pyt ...

  6. idea提升效率的插件

    这篇文章用于记录idea插件.多分类记录确实可以提升效率. 1. FindBugs 虽说Idea本身提供的代码检查工具已经很强大了,但Idea提供的更多是规范性的检查,如果需要深入地检查异常,可以使用 ...

  7. 尾递归(java)

    一般递归: 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多 ...

  8. NoSQLBench入门教程

    NoSQLBench发布于2020年3月,它是第一个试图在分布式系统性能测试上做到面面俱到的专业测试工具.同时,它旨在让轻量级的和专业的用户都可以使用. 什么是NoSQLBench? 当今的开发人员希 ...

  9. scp 转

    linux之cp/scp命令   名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... directory 说明 ...

  10. 分布式一致性算法 Paxos、Raft、Zab的区别与联系

    什么是分布式系统? 拿一个最简单的例子,就比如说我们的图书管理系统.之前的系统包含了所有的功能,比如用户注册登录.管理员功能.图书借阅管理等.这叫做集中式系统.也就是一个人干了好几件事. 后来随着功能 ...