poj 1127 简单几何+并查集
来源:互联网 发布:cpu淘宝 编辑:程序博客网 时间:2024/05/07 12:56
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.
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.
71 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 020 2 0 00 0 0 11 12 21 20 00
CONNECTED NOT CONNECTED CONNECTED CONNECTED NOT CONNECTED CONNECTEDCONNECTEDCONNECTEDCONNECTED
题意:
在桌子上给出一些线段,然后问你两条线段是否相交,有两种情况
(1)直接相交
(2)a,b相交,b,c相交,如果a,c不直接相交也算相交
思路:
首先一看是并查集的问题,然后需要判断两天线是否有一个祖节点,即判断两条直线是否相交
(1).快速排斥试验设以线段 P1P2 为对角线的矩形为R, 设以线段 Q1Q2 为对角线的矩形为T,如果R和T不相交,显然两线段不会相交;(2).跨立试验如果两线段相交,则两线段必然相互跨立对方,P1P2跨立Q1Q2 ,则矢量 ( P1 - Q1 ) 和( P2 - Q1 )位于矢量( Q2 - Q1 ) 的两侧,即( P1 - Q1 ) × ( Q2 - Q1 ) * ( P2 - Q1 ) × ( Q2 - Q1 ) < 0上式可改写成( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) > 0当( P1 - Q1 ) × ( Q2 - Q1 ) = 0 时,说明( P1 - Q1 ) 和 ( Q2 - Q1 )共线,但是因为已经通过快速排斥试验,所以 P1 一定在线段 Q1Q2上;同理,( Q2 - Q1 ) ×(P2 - Q1 ) = 0 说明 P2 一定在线段 Q1Q2上。所以判断P1P2跨立Q1Q2的依据是:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) ≥ 0同理判断Q1Q2跨立P1P2的依据是:( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) ≥ 0
ac:代码#include <iostream>
#include <cstdio>
using namespace std;
int pri[20];
struct point
{
int x;
int y;
};
struct line
{
point a;
point b;
}data[20];
int panduan(point a,point b,point c,point d)
{
if(max(a.x,b.x)<min(c.x,d.x)||max(a.y,b.y)<min(c.y,d.y)||max(c.x,d.x)<min(a.x,b.x)||max(c.y,d.y)<min(a.y,b.y))
return 0;
int h,i,j,k;
h = (a.x-c.x) * (d.y-c.y) - (a.y-c.y) * (d.x-c.x);
i = (d.x-c.x) * (b.y-c.y) - (d.y-c.y) * (b.x-c.x);
j = (c.x-a.x) * (b.y-a.y) - (c.y-a.y) * (b.x-a.x);
k = (b.x-a.x) * (d.y-a.y) - (b.y-a.y) * (d.x-a.x);
if(h*i>=0&&j*k>=0)
return 1;
else
return 0;
}
int find(int x)
{
if(x==pri[x])
return pri[x];
else
return pri[x]=find(pri[x]);
}
int main()
{
int n;
while(cin>>n&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d",&data[i].a.x,&data[i].a.y,&data[i].b.x,&data[i].b.y);
}
for(int i=1;i<=n;i++)
pri[i]=i;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(panduan(data[i].a,data[i].b,data[j].a,data[j].b))
{
int x=find(i);
int y=find(j);
if(x!=y)
pri[x]=y;
}
}
}
for(int i=1;i<=n;i++)
pri[i]=find(i);
int x1,y1;
while(~scanf("%d%d",&x1,&y1))
{
if(x1==0&&y1==0)
break;
if(pri[x1]==pri[y1])
printf("CONNECTED\n");
else
printf("NOT CONNECTED\n");
}
}
return 0;
}
0 0
- poj 1127 简单几何+并查集
- POJ 1127 计算几何+并查集
- POJ 2236 Wireless Network 几何+并查集
- poj 1558-Segment set (并查集+计算几何)
- 计算几何+并查集处理:POJ 1127 Jack Straws
- POJ 1127 基础计算几何(判断两线段相交)+并查集
- hdu1558(并查集和简单几何的结合)
- poj 1417(并查集+简单dp)
- poj 2403 简单并查集
- POJ 1308(简单并查集)
- POJ 2236(简单并查集)
- 简单并查集Wireless Network POJ
- Poj 并查集
- poj并查集
- POJ 并查集
- poj 2236 Wireless Network(并查集+一点点计算几何)
- hdu1558 几何处理 + 并查集
- poj2236(并查集+计算几何)
- java匿名内部类
- 30-换个姿势执行 ls 命令
- GitHub上README.md教程
- 一起talk C栗子吧(第一百九十四回:C语言实例--DIY less命令三 )
- 关于Faster R-CNN的一切——笔记2:Fast R-CNN
- poj 1127 简单几何+并查集
- 浮点数判断为0或者相等
- 实习每日总结_20161219
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
- Github访问慢解决办法
- mac os系统复制粘贴ctrl+c ctrl+v快捷键实现方式
- python Flask学习笔记(二)-集成bootstrap
- Shell中实现字符串反转
- 算法题1:单例模式