POJ 1127-Jack Straws(计算几何 线段相交)

一时失言乱红尘 2022-07-17 16:28 291阅读 0赞

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.

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

  1. 7
  2. 1 6 3 3
  3. 4 6 4 9
  4. 4 5 6 7
  5. 1 4 3 5
  6. 3 5 5 5
  7. 5 2 6 3
  8. 5 4 7 2
  9. 1 4
  10. 1 6
  11. 3 3
  12. 6 7
  13. 2 3
  14. 1 3
  15. 0 0
  16. 2
  17. 0 2 0 0
  18. 0 0 0 1
  19. 1 1
  20. 2 2
  21. 1 2
  22. 0 0
  23. 0

Sample Output

  1. CONNECTED
  2. NOT CONNECTED
  3. CONNECTED
  4. CONNECTED
  5. NOT CONNECTED
  6. CONNECTED
  7. CONNECTED
  8. CONNECTED
  9. CONNECTED

Source

East Central North America 1994

题目意思:

N根木棍,木棍i两端的坐标分别是(Pix,Piy)和(Qix,Qiy).

给出若干木棍(Ai,Bi),判断每对木棍是否相连。

当两根木棍之间有公共点时,就认为它们是相连的。通过相连的木棍间接连在一起的两根木棍也被认为是相连的。

解题思路:

木棍看成二维平面上的线段,求解交点再判断。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<cmath>
  5. #include<vector>
  6. #include<queue>
  7. #include<malloc.h>
  8. using namespace std;
  9. typedef long long ll;
  10. const int maxn = 10000;
  11. const int INF = 0x3f3f3f3f;
  12. double EPS=1e-10;
  13. double add(double a,double b)//double加法运算考虑误差
  14. {
  15. if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
  16. return a+b;
  17. }
  18. struct P//结构体定义二维向量
  19. {
  20. double x,y;
  21. P() {}
  22. P(double x,double y):x(x),y(y) {}
  23. P operator +(P p)
  24. {
  25. return P(add(x,p.x),add(y,p.y));
  26. }
  27. P operator -(P p)
  28. {
  29. return P(add(x,-p.x),add(y,-p.y));
  30. }
  31. P operator *(double d)
  32. {
  33. return P(x*d,y*d);
  34. }
  35. double dot(P p)//内积
  36. {
  37. return add(x*p.x,y*p.y);
  38. }
  39. double det(P p)//外积
  40. {
  41. return add(x*p.y,-y*p.x);
  42. }
  43. };
  44. bool on_seg(P p1,P p2,P q)//判断点q是否在线段p1-p2上
  45. {
  46. return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
  47. }
  48. P intersection(P p1,P p2,P q1,P q2)//计算直线p1-p2与q1-q2的交点
  49. {
  50. return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
  51. }
  52. int n,m;
  53. P p[maxn],q[maxn];
  54. int a[maxn],b[maxn];
  55. bool g[maxn][maxn];//相连关系图
  56. void solve()
  57. {
  58. int i,j,k;
  59. for(i=0; i<n; ++i)
  60. {
  61. g[i][i]=true;
  62. for(j=0; j<i; ++j)//判断木棍i和j是否有公共点
  63. {
  64. if((p[i]-q[i]).det(p[j]-q[j])==0)//平行
  65. 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]);
  66. else//相交
  67. {
  68. P r=intersection(p[i],q[i],p[j],q[j]);
  69. g[i][j]=g[j][i]=on_seg(p[i],q[i],r)&&on_seg(p[j],q[j],r);
  70. }
  71. }
  72. }
  73. for(k=0; k<n; ++k)//Floyd-Warshall算法判断任意两点间是否相连
  74. for(i=0; i<n; ++i)
  75. for(j=0; j<n; ++j)
  76. g[i][j] |= g[i][k] && g[k][j];
  77. for(i=0; i<m; ++i)
  78. puts(g[a[i]-1][b[i]-1]?"CONNECTED ":"NOT CONNECTED ");
  79. }
  80. int main()
  81. {
  82. while(1)
  83. {
  84. scanf("%d",&n);
  85. if(n==0) break;
  86. m=0;
  87. for(int i=0; i<n; ++i)
  88. scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&q[i].x,&q[i].y);
  89. while(1)
  90. {
  91. int aa,bb;
  92. scanf("%d%d",&aa,&bb);
  93. if(aa==0&&bb==0)break;
  94. a[m]=aa;
  95. b[m++]=bb;
  96. }
  97. solve();
  98. }
  99. return 0;
  100. }
  101. /**
  102. 7
  103. 1 6 3 3
  104. 4 6 4 9
  105. 4 5 6 7
  106. 1 4 3 5
  107. 3 5 5 5
  108. 5 2 6 3
  109. 5 4 7 2
  110. 1 4
  111. 1 6
  112. 3 3
  113. 6 7
  114. 2 3
  115. 1 3
  116. 0 0
  117. 2
  118. 0 2 0 0
  119. 0 0 0 1
  120. 1 1
  121. 2 2
  122. 1 2
  123. 0 0
  124. 0
  125. **/

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读