1840: Jack Straws

时间限制(普通/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.

样例输入

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

样例输出

CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED

题目来源

East Central North America 1994

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

 #include <bits/stdc++.h>  //1840: Jack Straws
using namespace std;
struct Point{
int x1,y1,x2,y2;
}A[];
int arr[]; struct Node{
int x,y;
};
bool judge(Point p1,Point p2){
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))
return false;
return true;
} int cultilate(Point p1,Point p2){
Node AB,AC,AD,CD,CB,CA;
AB.x=p1.x2-p1.x1,AB.y=p1.y2-p1.y1;
AC.x=p2.x1-p1.x1,AC.y=p2.y1-p1.y1;
AD.x=p2.x2-p1.x1,AD.y=p2.y2-p1.y1;
CD.x=p2.x2-p2.x1,CD.y=p2.y2-p2.y1;
CB.x=p1.x2-p2.x1,CB.y=p1.y2-p2.y1;
CA.x=p1.x1-p2.x1,CA.y=p1.y1-p2.y1;
if((AB.x*AC.y-AB.y*AC.x)*(AB.x*AD.y-AB.y*AD.x)<=&&(CD.x*CB.y-CD.y*CB.x)*(CD.x*CA.y-CD.y*CA.x)<=)
return ;
return ;
} int find_root(int x){
return arr[x]==x?x:arr[x]=find_root(arr[x]);
} int unionset(int x,int y){
int xx=find_root(x),yy=find_root(y);
if(xx!=yy){
if(xx>yy){
arr[yy]=xx;
}
else arr[xx]=yy;
}
} int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n&&n!=){
for(int i=;i<=;i++){
arr[i]=i;
}
for(int i=;i<=n;i++){
cin>>A[i].x1>>A[i].y1>>A[i].x2>>A[i].y2;
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
bool flag;
flag=judge(A[i],A[j]);
if(flag==true){
int zhi;
zhi=cultilate(A[i],A[j]);
if(zhi==){ //connect
unionset(i,j);
}
}
}
}
int d1,d2;
while(cin>>d1>>d2&&d1!=&&d2!=){
if(find_root(d1)==find_root(d2)){
cout << "CONNECTED" << endl;
}
else cout << "NOT CONNECTED" << endl;
}
}
return ;
}

1840: Jack Straws的更多相关文章

  1. TZOJ 1840 Jack Straws(线段相交+并查集)

    描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...

  2. TOJ 1840 Jack Straws

    Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  3. TOJ1840: Jack Straws 判断两线段相交+并查集

    1840: Jack Straws  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: 1 ...

  4. poj1127 Jack Straws(线段相交+并查集)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory L ...

  5. Jack Straws(poj 1127) 两直线是否相交模板

    http://poj.org/problem?id=1127   Description In the game of Jack Straws, a number of plastic or wood ...

  6. poj 1127:Jack Straws(判断两线段相交 + 并查集)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2911   Accepted: 1322 Descr ...

  7. Jack Straws POJ - 1127 (简单几何计算 + 并查集)

    In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...

  8. Jack Straws POJ - 1127 (几何计算)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5428   Accepted: 2461 Descr ...

  9. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

随机推荐

  1. Windows 下安装Git工具及基础使用

    Git简介 git是很好一个工具使用,可以执行liunx命令,有git环境后windows系统就可以进行shell命令操作,就可以添加其他liunx辅助软件进行执行,git也代码库管理工具,无论是上传 ...

  2. iOS -- Effective Objective-C 阅读笔记 (6)

    1: 在 既有类中使用 关联对象存放自定义数据 有时候需要在对象中存放相关信息, 这是我们经常会从对象所属的类中继承一个子类, 然后改用这个子类对象, 然而并非所有的情况下都能这么做,  有时候类的实 ...

  3. 基于netty的socket服务端触发了channelInactive方法,但实际连接没有断开的问题

    背景: 一个中小型H5游戏,后端使用基于 netty 的socket服务 服务端 分为 分发服务器 & 业务服务器,业务服务器可负载 用户客户端与分发服务器连接 分发服务器再作为客户端与每台业 ...

  4. 为什么用Flow

    Flow 是 facebook 出品的 JavaScript 静态类型检查工具.Vue.js 的源码利用了 Flow 做了静态类型检查,所以了解 Flow 有助于我们阅读源码. flow的工作方式? ...

  5. 常用Mysql或者PostGresql或者Greenplum的语句总结。

    1.使用mysql的union all可以同时查询出所有自己想要查询数据表的数据量. select 'user' as tablename, count(*) from user union all ...

  6. Spring 源码分析-1-启动

    Spring 源码分析-1-启动 在web项目中使用spring的时候,我们会在web.xml中加入如下配置: <listener> <listener-class>org.s ...

  7. Pyqt walk 在Windows查找文件

    在任意目录下查找需要的文件如何操作呢? 其实很简单, WIN+E [桌面计算机]- 右上角“搜索 计算机” 这个就是Windows自带的文件搜索功能.自己做一个文件搜索的应该应该也挺好玩的. 知识要点 ...

  8. python输入

    (程序是如何输入输出的) 先了解一个概念,什么是函数? 简单来说,函数就是封装了一些功能,到时候只需要写一个函数名字,就可以使用这些功能 input函数,它是输入函数,它可以将用户输入的内容当做“字符 ...

  9. vue中mint-ui的filed的与blur事件结合实现检查用户输入是否正确

    标题mint-ui的filed与blur事件验证用户输入格式是否正确说明:本人前端菜鸟,只是想借个地方做个笔记,为了以后查阅时比较方便.如有大神有什么建议的地方,欢迎提出来. 1.不得不说,mint- ...

  10. Java运行原理、三大体系、jdk构成

    一.java运行原理: 二.Java分为三个体系: JavaSE(J2SE)(Java2 Platform Standard Edition,java平台标准版) JavaEE(J2EE)(Java ...