POJ 1127 Jack Straws 几何基础

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个木棒,两端分别是(x1,y1),(x2,y2),但1木棒与2木棒相交,2木棒与3木棒相交时,说明1木棒与3木棒也相交有若干个询问a和b,求a与b是否相交。判断两个木棒是否相交,可以想判断他们是否平行,平行的话直接其中一个木棒的一点在另一木棒上就是相交,不平行的话,求两个直线的交点,看这个交点是否在这两个木棒上在的话就是相交了。下面是代码,根据挑战程序设计上的计算几何基础写的。
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <cmath>
 5 using namespace std;
 6 const int N = 20;
 7 const int M = 110;
 8 double EPS = 1e-10;
 9 double add(double a,double b) {
10     if(abs(a+b) < EPS * (abs(a) + abs(b))) return 0;
11     return a + b;
12 }
13 struct Point{
14     double x, y;
15     Point(){}
16     Point(double x, double y) :x(x), y(y) {}
17     Point operator + (Point p) {
18         return Point(add(x, p.x), add(y, p.y));
19     }
20     Point operator - (Point p) {
21         return Point(add(x, -p.x), add(y, -p.y));
22     }
23     Point operator * (double d) {
24         return Point(x*d, y*d);
25     }
26     double dot(Point p) {    //内积
27         return add(x * p.x, y * p.y);
28     }
29     double det(Point p) {    //外积
30         return add(x * p.y, -y * p.x);
31     }
32 };
33
34 bool on_seg(Point p1, Point p2, Point q) {      //判断点q是否在线段p1-p2上
35     return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
36 }
37 Point intersection(Point p1, Point p2, Point q1, Point q2) {    //计算直线p1-p2与直接q1-q2的交点
38     return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
39 }
40 int n;
41 Point p[N], q[N];
42 int m;
43 bool g[N][N];
44 void solve() {
45     for(int i = 1; i <= n; i ++) {
46         g[i][i] = true;
47         for(int j = 1; j < i; j ++) {
48             if((p[i] - q[i]).det(p[j] - q[j]) == 0) {
49                 g[i][j] = g[j][i] = on_seg(p[i], q[i], q[j])
50                                     || on_seg(p[i], q[i], p[j])
51                                     || on_seg(p[j], q[j], q[i])
52                                     || on_seg(p[j], q[j], p[i]);
53             } else {
54                 Point r = intersection(p[i], q[i], p[j], q[j]);
55                 g[i][j] = g[j][i] = on_seg(p[i], q[i], r) && on_seg(p[j], q[j], r);
56             }
57         }
58     }
59     for(int k = 1; k <= n; k ++) {
60         for(int i = 1; i <= n; i ++) {
61             for(int j = 1; j <= n; j ++) {
62                 g[i][j] |= g[i][k] && g[k][j];
63             }
64         }
65     }
66 }
67 int main() {
68     while(cin >> n && n) {
69         memset(g, false, sizeof(g));
70         for(int i = 1; i <= n; i ++) {
71             cin >> p[i].x >> p[i].y >> q[i].x >> q[i].y;
72         }
73         int a, b;
74         solve();
75         while(cin >> a >> b && (a+b)) {
76             if(g[a][b]) printf("CONNECTED\n");
77             else printf("NOT CONNECTED\n");
78         }
79     }
80     return 0;
81 }
时间: 2024-11-03 22:49:03

POJ 1127 Jack Straws 几何基础的相关文章

POJ 1127 Jack Straws (计算几何)

Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3945   Accepted: 1787 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 witho

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

Jack Straws 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

poj 1127 Jack Straws 线段相交+并查集

题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <map> #include <

POJ 1127 Jack Straws (线段相交)

题意:给定一堆线段,然后有询问,问这两个线段是不是相交,并且如果间接相交也可以. 析:可以用并查集和线段相交来做,也可以用Floyd来做,相交就是一个模板题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #includ

POJ - 1127 Jack Straws(几何)

题意:桌子上放着n根木棍,已知木棍两端的坐标.给定几对木棍,判断每对木棍是否相连.当两根木棍之间有公共点或可以通过相连的木棍间接的连在一起,则认为是相连的. 分析: 1.若线段i与j平行,且有部分重合,则相连.否则,求直线i与直线j交点,再判断该交点是否在两线段上,以确定是否相连. 2.flod整理一下所有的关系. 3.判断点是否在线段上:(线段i,点t) (1)外积(l[i] - t) × (r[i] - t) = 0, 可判断点x是否在直线i上(两向量叉乘为0,两向量平行) (2)内积(l[

POJ 1127 Jack Straws ( 求直线交点, 判断线段是否相交(含端点) )

题目:传送门 题意: 给你 n 条线段的两个端点, 然后有多次询问, 每次询问, 问你线段 x 和 线段 y 是否相交. 若线段 A 和线段 B 相交且线段 A 和线段 C 相交,那么线段 B 和线段 C 相交.     1 < n < 13 题解: 暴力求线段是否相交, 然后再跑个 Floyd 或者并查集都可以的. #include <iostream> #include <stdio.h> #include <string.h> #include <

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

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3512   Accepted: 1601 Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped o

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

Jack Straws 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 o

Jack Straws(并差集和判断直线相交问题)

Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3155   Accepted: 1418 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 witho