poj 1127 判断任意两线段是否相交,叉积+floyd(并查集)@
2017-11-02 17:50
549 查看
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
Sample Output
Source
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4342 | Accepted: 1976 |
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
Source
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <algorithm> using namespace std; const int N = 1e4+10; struct node { double x1, y1, x2, y2; } p ; const double eps=1e-10; double det(int i,double x,double y)//外积 { double x1=p[i].x1-x, y1=p[i].y1-y; double x2=p[i].x2-x, y2=p[i].y2-y; return add(x1*y2,-x2*y1); } int judge(int i,int j) { int l1=min(p[i].x1,p[i].x2), l2=max(p[i].x1,p[i].x2); int l3=min(p[i].y1,p[i].y2), l4=max(p[i].y1,p[i].y2); int l5=min(p[j].x1,p[j].x2), l6=max(p[j].x1,p[j].x2); int l7=min(p[j].y1,p[j].y2), l8=max(p[j].y1,p[j].y2); if(l2<l5||l6<l1||l4<l7||l8<l3) return 0; return (det(i,p[j].x1,p[j].y1)*det(i,p[j].x2,p[j].y2))<=0&&(det(j,p[i].x1,p[i].y1)*det(j,p[i].x2,p[i].y2)<=0); } int w[200][200]; int main() { int n; while(scanf("%d", &n),n!=0) { for(int i=1; i<=n; i++) { scanf("%lf %lf %lf %lf", &p[i].x1, &p[i].y1, &p[i].x2, &p[i].y2); } memset(w,0,sizeof(w)); for(int i=1; i<=n; i++) { for(int j=i; j<=n; j++) if(judge(i,j)) w[i][j]=w[j][i]=1; } for(int k=1; k<=n; k++) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { w[i][j]|=(w[i][k]&&w[k][j]); w[j][i]=w[i][j]; } } } int x, y; while(scanf("%d %d", &x, &y),x!=0||y!=0) { if(w[x][y]) printf("CONNECTED\n"); else printf("NOT CONNECTED\n"); } } return 0; }
相关文章推荐
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
- poj 1127 Jack Straws 并查集+线段规范相交的判断
- POJ 1127 Jack Straws(线段相交判断+并查集)
- 【计算几何】 POJ 1127 Jack Straws 判断线段是否相交
- 【计算几何】 POJ 1127 Jack Straws 判断线段是否相交
- POJ 1127 Jack Straws(线段相交判断+并查集)
- POJ 1127 基础计算几何(判断两线段相交)+并查集
- POJ - 3304 :Segments__判断直线和线段是否 相交
- 判断平面上任意两条线段是否相交-Python实现
- poj 1410 Intersection 【判断线段 与矩形面是否相交】
- ccsu1359 木棒相交 (叉积线段判交,并查集判断是否属于同一个集合)
- poj 3304 判断是否存在一条直线与所有线段相交
- POJ 1410 Intersection(判断线段与矩形是否相交)
- POJ 3304 Segments (计算几何、判断直线与线段是否相交)
- [poj 1127]Jack Straws[线段相交][并查集]
- 【算法导论】33.2:判断任意两条线段是否相交
- POJ 1127 Jack Straws (线段相交+并查集)
- 判断同一平面内任意两线段是否相交的方法
- POJ 1066 Treasure Hunt 判断两线段是否相交
- POJ_3304_Segments_线段判断是否相交