Segments

http://poj.org/problem?id=3304

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18066   Accepted: 5680

Description

Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1y1) and (x2y2) are the coordinates of the two endpoints for one of the segments.

Output

For each test case, your program must output "Yes!", if a line with desired property exists and must output "No!" otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

Sample Input

3
2
1.0 2.0 3.0 4.0
4.0 5.0 6.0 7.0
3
0.0 0.0 0.0 1.0
0.0 1.0 0.0 2.0
1.0 1.0 2.0 1.0
3
0.0 0.0 0.0 1.0
0.0 2.0 0.0 3.0
1.0 1.0 2.0 1.0

Sample Output

Yes!
Yes!
No! 枚举每两个线段的端点,判断这些端点形成的直线是否与线段都有交点
还有,题目说如果两个点的距离小于1e-8就看成一个点,所以要考虑重点
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std; struct Point{
double x,y;
}; struct Line{
Point s,e;
}line[]; double Cross(double x0,double y0,double x1,double y1,double x2,double y2){
return (x1-x0)*(y2-y0)-(y1-y0)*(x2-x0);
} double distance(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
} bool Check(double x1,double y1,double x2,double y2,int n){
if(distance(x1,y1,x2,y2)<0.00000001) return ;
for(int i=;i<=n;i++){
if(Cross(x1,y1,x2,y2,line[i].s.x,line[i].s.y)*Cross(x1,y1,x2,y2,line[i].e.x,line[i].e.y)>0.00000001){
return ;
}
}
return ;
} bool ac(int n){
Line tmp;
int co=;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
tmp.s=line[i].s;
tmp.e=line[j].s;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].s;
tmp.e=line[j].e;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].e;
tmp.e=line[j].s;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
tmp.s=line[i].e;
tmp.e=line[j].e;
if(Check(tmp.s.x,tmp.s.y,tmp.e.x,tmp.e.y,n)){
co=;
}
} }
if(co)
return true;
return false;
} int main(){ int T;
cin>>T;
while(T--){
int n;
cin>>n;
Point s,e;
for(int i=;i<=n;i++){
cin>>s.x>>s.y>>e.x>>e.y;
line[i].s=s;
line[i].e=e;
}
if(ac(n)){
puts("Yes!");
}
else{
puts("No!");
}
} }

Segments(叉积)的更多相关文章

  1. POJ 3304 Segments 叉积

    题意: 找出一条直线,让给出的n条线段在这条直线的投影至少有一个重合的点 转化一下,以重合的点作垂线,那么这条直线一定经过那n条线段.现在就是求找到一条直线,让这条直线经过所有线段 分析: 假设存在这 ...

  2. POJ 3304 Segments【叉积】

    题意:有n条线段,问有没有一条直线使得所有线段在这条直线上的投影至少有一个共同点. 思路:逆向思维,很明显这个问题可以转化为是否有一条直线穿过所有线段,若有,问题要求的直线与该直线垂直,并且公共点为垂 ...

  3. poj 3304 Segments

    Segments 题意:给你100以内的n条线段,问你是否存在一条直线,使得题给的线段在这条直线上的“投影” 相交于一点: 思路: 1.先要将线段投影相交于一点转变为存在一条直线与所有的线段相交: 很 ...

  4. POJ 3304 Segments[直线与线段相交]

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13514   Accepted: 4331 Descrip ...

  5. (叉积,线段判交)HDU1086 You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  6. POJ 3304 Segments(线的相交判断)

    Description Given n segments in the two dimensional space, write a program, which determines if ther ...

  7. POJ 3304 Segments 判断直线和线段相交

    POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...

  8. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  9. Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor

    结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...

随机推荐

  1. Win10巧用自带输入法轻松打出特殊字符

    给电脑输入信息时,如果要用上键盘上没有的特殊符号,那就为难了.其实,在Win10中,自带的微软拼音就能让你轻松输入键盘上没有的符号.下面来看看Win10如何输入特殊符号. 微软拼音不但中文输入智能化做 ...

  2. storm的定时任务

    应用场景: 第一种方法 参考代码StormTopologyTimer1.java package yehua.storm; import java.util.Map; import org.apach ...

  3. 使用 Nmon 监控 Linux 的系统性能

    Nmon(得名于 Nigel 的监控器)是IBM的员工 Nigel Griffiths 为 AIX 和 Linux 系统开发的一款计算机性能系统监控工具.Nmon 可以把操作系统的统计数据展示在屏幕上 ...

  4. Django中组合搜索功能

    需求分析 很多电商网站中有组合搜索的功能,所谓组合搜索就是网页中组合多个条件,对数据库中进行查询,并且将结果显示在页面中,看个例子吧: 注意红框中的标识,我们可以根据URL来做组合搜索. video- ...

  5. mysql:rand()产生随机整数,CONCAT拼接时间字符串

    用存储过程插入测试数据,如果不想update_time都一样,可以进行随机字符串拼接:2月随机1天,小时随机 CONCAT('2017-02-',FLOOR(1 + (RAND() * 28)),' ...

  6. sql 数据库修复

    数据库修复 exec sp_dboption 'dbname1','single user',‘true’ dbcc checkdb('dbname1') dbcc checkdb('dbname1' ...

  7. web.xml 组件加载顺序

    <web-app>     <display-name></display-name> WEB应用的名字 <description></descr ...

  8. jquery 阻止冒泡事件和阻止默认事件

    jQuery 冒泡和默认事件: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  9. Linux多网卡负载均衡 : bond

    USERCTL=no/yes 普通用户是否可以关闭或启用网关 -------------------------------- 在这介绍的Linux双网卡绑定实现就是使用两块网卡虚拟成为一块网卡,这个 ...

  10. 进入一个docker容器

    Starting from Docker 1.3 you can use Docker exec to enter a Docker container : docker exec -it CONTA ...