[ACM] hdu Jack Straws

2024-01-28 13:58
文章标签 acm hdu jack straws

本文主要是介绍[ACM] hdu Jack Straws,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Jack Straws

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size:  

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 02
0 2 0 0
0 0 0 1
1 1
2 2
1 2
0 00

Sample Output

CONNECTED 
NOT CONNECTED 
CONNECTED 
CONNECTED 
NOT CONNECTED 
CONNECTED
CONNECTED
CONNECTED
CONNECTED

Author

YTlearner

Source

East Central North America 1994


解题思路:

题目关键点是判断两线段是否相交和并查集的使用。两个线段可以直接相交也可以通过其它线段间接相交,题目中的意思是不管两个线段是直接还是间接相交,都输出CONNECTED ,否则输出 NOT CONNECTED。并查集的使用是把所有相交的线段放在同一个集合里面(包括直接相交和间接相交),查询的时候,如果两个线段在同一集合(有相同的根节点)就是相交的情况。

代码:

#include <iostream>
#include <algorithm>
using namespace std;struct point//构造点
{int x,y;
};
struct line//构造线段
{point a,b;
};double multi(point p1,point p2,point p0)//矩形面积
{return((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));
}bool intersect(line u,line v)//判断两线段是否相交
{return( (max(u.a.x,u.b.x)>=min(v.a.x,v.b.x))&&(max(v.a.x,v.b.x)>=min(u.a.x,u.b.x))&&(max(u.a.y,u.b.y)>=min(v.a.y,v.b.y))&&(max(v.a.y,v.b.y)>=min(u.a.y,u.b.y))&&(multi(v.a,u.b,u.a)*multi(u.b,v.b,u.a)>=0)&&(multi(u.a,v.b,v.a)*multi(v.b,u.b,v.a)>=0));
}int parent[14];//父节点
int rank[14];//树高
int n;
line lin[14];//线段数void init(int n)//初始化集合,每条线段都是一个独立的集合,树高全部为0
{for(int i=1;i<=n;i++){parent[i]=i;rank[i]=0;}
}int find(int x)//找根节点
{return parent[x]==x?x:find(parent[x]);
}void unite(int x,int y)//把两个集合合并,树高(rank)小的向大的那边合并。
{x=find(x);y=find(y);if(x==y)return;if(rank[x]<rank[y])parent[x]=y;else{parent[y]=x;if(rank[x]==rank[y])rank[x]++;}}bool same(int x,int y)//判断两条线段是否有相同的根节点,也就是判断是否属于同一集合,同一集合的线段都相连(直接相连和间接相连)
{return find(x)==find(y);
}
int main()
{while(cin>>n&&n){for(int i=1;i<=n;i++){cin>>lin[i].a.x>>lin[i].a.y>>lin[i].b.x>>lin[i].b.y;}init(n);for(int i=1;i<=n;i++)//集合合并过程for(int j=i+1;j<=n;j++){if(intersect(lin[i],lin[j]))//如果第i条线段和第j条线段相交unite(i,j);}int l1,l2;while(cin>>l1>>l2&&l1&&l2){if(same(l1,l2))cout<<"CONNECTED"<<endl;elsecout<<"NOT CONNECTED"<<endl;}}return 0;
}

运行:


这篇关于[ACM] hdu Jack Straws的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/653831

相关文章

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

hdu 1102 uva 10397(最小生成树prim)

hdu 1102: 题意: 给一个邻接矩阵,给一些村庄间已经修的路,问最小生成树。 解析: 把已经修的路的权值改为0,套个prim()。 注意prim 最外层循坏为n-1。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstri