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;}
- POJ 1127(Jack Straws )(线段相交)
- POJ 1127 Jack Straws (线段相交 Floyd)
- POJ 1127 Jack Straws (线段相交+并查集)
- POJ 1127-Jack Straws(计算几何 线段相交)
- [poj 1127]Jack Straws[线段相交][并查集]
- POJ 1127 Jack Straws(线段相交判断+并查集)
- 【计算几何】 POJ 1127 Jack Straws 判断线段是否相交
- POJ 1127Jack Straws (计算几何 + 线段相交)
- POJ 1127 Jack Straws (判断两线段相交)
- POJ 1127 Jack Straws(线段相交判断+并查集)
- TOJ 1840 POJ 1127 HDU 1102 Jack Straws / 线段相交 + 并查集
- POJ 1127 Jack Straws (线段不规范相交&&图的连通性&&Floyd-Warshall算法)
- poj 1127 Jack Straws 并查集+线段规范相交的判断
- poj1127—Jack Straws(线段相交+并查集)
- POJ 1127 Jack Straws
- poj 1127 Jack Straws
- poj 1127 Jack Straws
- poj--1127 Jack Straws
- python遇到UnicodeDecodeError错误的解决办法
- 自定义控件其实很简单 三
- https 之 ssl安全证书的获取和安装
- shiro简单配置
- 配置pacman源
- POJ 1127(Jack Straws )(线段相交)
- 如何在 Linux 上录制你的终端操作
- Linux给用户添加sudo权限
- Linux umount 报 device is busy 的处理方法
- 如何快速转载CSDN中的博客
- 面向对象2
- 多线程的等待/通知机制
- discuz学习笔记:显示数组内容、显示二维数组特定值
- 笔记 9.19