POJ 1127-Jack Straws(計算幾何 線段相交)
Jack Straws
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4120 | Accepted: 1874 |
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.
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
題目意思:
N根木棍,木棍i兩端的座標分別是(Pix,Piy)和(Qix,Qiy).
給出若干木棍(Ai,Bi),判斷每對木棍是否相連。
當兩根木棍之間有公共點時,就認為它們是相連的。通過相連的木棍間接連在一起的兩根木棍也被認為是相連的。
解題思路:
木棍看成二維平面上的線段,求解交點再判斷。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
#include<malloc.h>
using namespace std;
typedef long long ll;
const int maxn = 10000;
const int INF = 0x3f3f3f3f;
double EPS=1e-10;
double add(double a,double b)//double加法運算考慮誤差
{
if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
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)//判斷點q是否線上段p1-p2上
{
return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
}
P intersection(P p1,P p2,P q1,P q2)//計算直線p1-p2與q1-q2的交點
{
return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}
int n,m;
P p[maxn],q[maxn];
int a[maxn],b[maxn];
bool g[maxn][maxn];//相連關係圖
void solve()
{
int i,j,k;
for(i=0; i<n; ++i)
{
g[i][i]=true;
for(j=0; j<i; ++j)//判斷木棍i和j是否有公共點
{
if((p[i]-q[i]).det(p[j]-q[j])==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);
}
}
}
for(k=0; k<n; ++k)//Floyd-Warshall演算法判斷任意兩點間是否相連
for(i=0; i<n; ++i)
for(j=0; j<n; ++j)
g[i][j] |= g[i][k] && g[k][j];
for(i=0; i<m; ++i)
puts(g[a[i]-1][b[i]-1]?"CONNECTED ":"NOT CONNECTED ");
}
int main()
{
while(1)
{
scanf("%d",&n);
if(n==0) break;
m=0;
for(int i=0; i<n; ++i)
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&q[i].x,&q[i].y);
while(1)
{
int aa,bb;
scanf("%d%d",&aa,&bb);
if(aa==0&&bb==0)break;
a[m]=aa;
b[m++]=bb;
}
solve();
}
return 0;
}
/**
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
**/
相關文章
- 【計算幾何】線段相交
- POJ 1039-Pipe(計算幾何-線段相交、求交點)
- 【計算幾何】求線段相交交點座標
- POJ 2991 Crane(線段樹+計算幾何)
- POJ 4048 Chinese Repeating Crossbow(線段相交)ROS
- POJ 1584-A Round Peg in a Ground Hole(計算幾何-凸包、點到線段距離)
- POJ - 1556 【計算幾何 + 最短路】
- An Easy Problem?! POJ 2826 計算幾何
- HDU 4643 GSM(計算幾何求線段的中垂線)
- [計算幾何]圓與三角形是否相交
- POJ 1556 The Doors(Dijkstra+計算幾何)
- POJ 1113 Wall(思維 計算幾何 數學)
- 計算幾何
- 【計算幾何】點定位(線段,三角形,多邊形)
- 計算幾何:模板
- [筆記] 計算幾何筆記
- 【計算幾何】向量表示
- 【總結】計算幾何模板
- 二維計算幾何模板
- 三維計算幾何模板
- BZOJ 1043: [HAOI2008]下落的圓盤 計算幾何,貪心,線段交
- 計算幾何 —— 二維幾何基礎 —— 距離度量方法
- 邊緣計算、霧計算、雲端計算區別幾何?
- 【學習筆記】計算幾何筆記
- 計算幾何_向量的實現
- 【計算幾何】多邊形交集
- POJ 3335-Rotating Scoreboard(計算幾何-半平面交順時針模板)
- 計算幾何常用的函式/方法函式
- POJ 1408-Fishnet(計算幾何-根據交點求多邊形面積)
- BNUOJ 12887 isumi(計算幾何+最大流)
- SGU 124 Broken line(計算幾何)
- 【計算幾何】Triangles HUST 1607
- 【計算幾何】多邊形點集排序排序
- C++計算幾何演算法大全C++演算法
- 【計算幾何】點在多邊形內部
- POJ 3130-How I Mathematician Wonder What You Are!(計算幾何-星形-半平面交逆時針模板)
- 【第一道計算幾何題】 UVA11178 Morley‘s Theorem (二維幾何,旋轉直線求求交點)REM
- 二維幾何常用運算