1840: Jack Straws

1840: Jack Straws 分享至QQ空间

时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte
总提交: 168            测试通过:129

描述

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 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.

输出

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.

样例输入

样例输出

题目来源

East Central North America 1994

解题思路:判断2条直线是不是相交+并查集

 

 1 #include <bits/stdc++.h>  //1840: Jack Straws
 2 using namespace std;
 3 struct Point{
 4     int x1,y1,x2,y2;
 5 }A[20];
 6 int arr[20];
 7 
 8 struct Node{
 9     int x,y;
10 };
11 bool judge(Point p1,Point p2){
12     if(max(p1.x1,p1.x2)<min(p2.x1,p2.x2)||max(p1.y1,p1.y2)<min(p2.y1,p2.y2)||min(p1.x1,p1.x2)>max(p2.x1,p2.x2)||min(p1.y1,p1.y2)>max(p2.y1,p2.y2))
13         return false;
14     return true;
15 }
16 
17 int cultilate(Point p1,Point p2){
18     Node AB,AC,AD,CD,CB,CA;
19     AB.x=p1.x2-p1.x1,AB.y=p1.y2-p1.y1;
20     AC.x=p2.x1-p1.x1,AC.y=p2.y1-p1.y1;
21     AD.x=p2.x2-p1.x1,AD.y=p2.y2-p1.y1;
22     CD.x=p2.x2-p2.x1,CD.y=p2.y2-p2.y1;
23     CB.x=p1.x2-p2.x1,CB.y=p1.y2-p2.y1;
24     CA.x=p1.x1-p2.x1,CA.y=p1.y1-p2.y1;
25     if((AB.x*AC.y-AB.y*AC.x)*(AB.x*AD.y-AB.y*AD.x)<=0&&(CD.x*CB.y-CD.y*CB.x)*(CD.x*CA.y-CD.y*CA.x)<=0)
26         return 1;
27     return 0;
28 }
29 
30 int find_root(int x){
31     return arr[x]==x?x:arr[x]=find_root(arr[x]);
32 }
33 
34 int unionset(int x,int y){
35     int xx=find_root(x),yy=find_root(y);
36     if(xx!=yy){
37         if(xx>yy){
38             arr[yy]=xx;
39         }
40         else arr[xx]=yy;
41     }
42 }
43 
44 int main()
45 {
46     ios::sync_with_stdio(false);
47     int n;
48     while(cin>>n&&n!=0){
49         for(int i=1;i<=19;i++){
50             arr[i]=i;
51         }
52         for(int i=1;i<=n;i++){
53             cin>>A[i].x1>>A[i].y1>>A[i].x2>>A[i].y2;
54         }
55         for(int i=1;i<=n;i++){
56             for(int j=i+1;j<=n;j++){
57                 bool flag;
58                 flag=judge(A[i],A[j]);
59                 if(flag==true){
60                     int zhi;
61                     zhi=cultilate(A[i],A[j]);
62                     if(zhi==1){   //connect
63                         unionset(i,j);
64                     }
65                 }
66             }
67         }
68         int d1,d2;
69         while(cin>>d1>>d2&&d1!=0&&d2!=0){
70             if(find_root(d1)==find_root(d2)){
71                 cout << "CONNECTED" << endl;
72             }
73             else cout << "NOT CONNECTED" << endl;
74         }
75     }
76     return 0;
77 }
View Code

 

 

原文地址:https://www.cnblogs.com/qq-1585047819/p/10701955.html