最新消息:点击查看大S的省钱秘笈

POJ 1127 Jack Straws C语言版

OJ题解 Slyar 3021浏览 0评论

文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作。

计算几何

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

Slyar:说下题目大意。首先给出一个n,表示共有n条边,下面n行(编号1-n)每行给出4个数分别表示起始点的横纵坐标以及终止点的横纵坐标。接下来的若干行为查询条件,要你输出查询的两条边是否有关系(即直接相交或间接相交)。

我讨厌做计算几何题...这题主要就是判断由坐标给出的两条边是否相交,因为描述说可以间接相交,所以考虑使用并查集处理(如果两条边相交,就将它们合并)。

至于如何判断两条边相交...这个我不会,也没想出来,直接网上搜...发现一个利用"叉积"来进行"跨立判断"的公式,没看懂,所以就不讲了...等我研究清楚再专门发文章吧...囧

处理完所有的边后,查询就简单了,只要看被查询的两条边是否在同一个集合即可。如果在,则有关系;如果不在,则没有关系。

转载请注明:Slyar Home » POJ 1127 Jack Straws C语言版

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址