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

Segments
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9449   Accepted: 2902

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!

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

简短的题目描述,但题目确实挺难的

别人的题解很详细,我就不献丑了,下面题解的连接:http://hi.baidu.com/cloudayc/item/243f3c4c2a687aaadf2a9fb7

证明:坐标系内若干线段,是否存在一条直线与每条线段都有交点

转成叉积计算线段和直线是否相交,注意重点

转自discuss上的证明:from hanjialong

首先题中的要求等价于:存在一条直线l和所有的线段都相交。
证明:若存在一条直线l和所有线段相交,作一条直线m和l垂直,则m就是题中要求的直线,所有线段投影的一个公共点即为垂足。(l和每条线段的交点沿l投影到m上的垂足处)
反过来,若存在m,所有线段在m上的投影有公共点,则过这点垂直于m作直线l,l一定和所有线段相交。

然后证存在l和所有线段相交等价于存在l过某两条线段的各一个端点且和所有线段相交。
充分性显然。必要性:若有l和所有线段相交,则可保持l和所有线段相交,左右平移l到和某一线段交于端点停止(“移不动了”)。然后绕这个交点旋转。也是转到“转不动了”(和另一线段交于其一个端点)为止。这样就找到了一个新的l。

于是本题可归结为枚举两两线段的各一个端点,连一条直线,再判断剩下的线段是否都和这条直线有交点。

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h> using namespace std;
#define eps 1e-8
#define MAXX 1010 typedef struct point
{
double x;
double y;
}point; typedef struct line
{
point st;
point ed;
}line; point p[MAXX];
line li[MAXX]; bool dy(double x,double y){ return x>y+eps; }
bool xy(double x,double y){ return x<y-eps; }
bool dyd(double x,double y){ return x>y-eps; }
bool xyd(double x,double y){ return x<y+eps; }
bool dd(double x,double y){ return fabs(x-y)<eps; } double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
} double dist(point a,point b)
{
return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
} bool onSegment(point a,point b,point c)
{
double maxx=max(a.x,b.x);
double maxy=max(a.y,b.y);
double minx=min(a.x,b.x);
double miny=min(a.y,b.y);
if(dd(crossProduct(a,b,c),0.0) && xyd(c.x,maxx) && dyd(c.x,minx) && xyd(c.y,maxy) && dyd(c.y,miny))
return true;
return false;
} bool segIntersect(point p1,point p2,point p3,point p4)
{
double d1=crossProduct(p3,p4,p1);
double d2=crossProduct(p3,p4,p2);
double d3=crossProduct(p1,p2,p3);
double d4=crossProduct(p1,p2,p4);
/*if(xy(d1*d2,0.0) && xy(d3*d4,0.0))
return true;
if(dd(d1,0.0) && onSegment(p3,p4,p1))
return true;
if(dd(d2,0.0) && onSegment(p3,p4,p2))
return true;
if(dd(d3,0.0) && onSegment(p1,p2,p3))
return true;
if(dd(d4,0.0) && onSegment(p1,p2,p4))
return true;
return false;*/
return xyd(d1 * d2,0.0);
} int main()
{
int n,m,i,j,t;
//freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int sub=,suc=;
for(i=; i<n; i++)
{
scanf("%lf%lf%lf%lf",&p[sub].x,&p[sub].y,&p[sub+].x,&p[sub+].y);
sub += ;
li[suc].st.x=p[sub-].x;
li[suc].st.y=p[sub-].y;
li[suc].ed.x=p[sub-].x;
li[suc++].ed.y=p[sub-].y;
}
if(n == || n == )
{
printf("Yes!\n");
continue;
}
bool flag=false;
for(i=; i<sub; i++)
{
for(j=i+; j<sub; j++)
{
int sum=;
if(dist(p[i],p[j]) < eps)
continue;
for(int k=; k<suc; k++)
{
if(!segIntersect(li[k].st,li[k].ed,p[i],p[j]))
{
break;
}
else
sum++;
}//printf("%d***",sum);//test
if(sum >= n)
{
flag=true;
goto end;
}
//else sum=0;
}
}
end:;
if(flag)printf("Yes!\n");
else printf("No!\n");
}
return ;
}

poj 3304线段与直线相交的更多相关文章

  1. 判断线段和直线相交 POJ 3304

    // 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...

  2. POJ 3304 Segments (判断直线与线段相交)

    题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...

  3. POJ 1039 Pipe【经典线段与直线相交】

    链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

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

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

  5. POJ 3304 Segments (直线与线段是否相交)

    题目链接 题意 : 能否找出一条直线使得所有给定的线段在该直线上的投影有一个公共点. 思路 : 假设存在一条直线a使得所有线段在该直线上的投影有公共点,则必存在一条垂直于直线a的直线b,直线b与所有线 ...

  6. POJ 3304 Segments (直线和线段相交判断)

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7739   Accepted: 2316 Descript ...

  7. poj 3304 Segments 线段与直线相交

    Segments Time Limit: 1000MS   Memory Limit: 65536K       Description Given n segments in the two dim ...

  8. poj 1269 Intersecting Lines(直线相交)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 391 ...

  9. POJ 3304 segments 线段和直线相交

    Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14178   Accepted: 4521 Descrip ...

随机推荐

  1. selenium定位失败记录

    selenium webdriver定位不到元素的五种原因及解决办法 1.动态id定位不到元素 for example: //WebElement xiexin_element = driver.fi ...

  2. rsync 远程数据同步工具详解

    rysnc 命令用法:(OPTION-参数,USER-用户,HOST-IP地址,SRC-复制源位置,DEST-复制目标位置)Shell拉:rsync [OPTION] [USER@]HOST:SRC ...

  3. Linux 进程状态【转】

    转自:http://www.cnblogs.com/itech/p/3208261.html 来自: http://blog.csdn.net/tianlesoftware/article/detai ...

  4. telnet不通11211端口,防火墙

    问题描述: 按照官网步骤,虚拟机里安装并启动memcached, 虚拟机里自己telnet11211端口可以连接, 使用Xmanager22端口可以连接到虚拟机,但是始终telnet不同11211端口 ...

  5. MYSQL的主从和主主复制模式

    一.复制介绍 MySQL支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环.这些日志可以记录 ...

  6. File和URL的getPath()方法区别

    java.io.File对象的getPath()方法返回文件的全路径名.如果是目录返回目录路径且结尾没有"\".如果是文件包含文件名. java.io.File对象的getName ...

  7. 最简单的C/S程序——让服务器来做加法

    还在写“Hello world!”式的单机程序吗?还在各种拖控件吗?是否自己都觉得有点low呢?来个质的飞跃吧!看看怎么让服务器帮咱做加法! 所谓C/S程序就是Client/Server程序,自然既包 ...

  8. (POJ2635)The Embarrassed Cryptographer(大数取模)

    The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13041 Accep ...

  9. SqlServer性能优化和工具Profiler(转)

    合理的优化和熟练的运用Profiler会让你更好的掌握系统的sql语句和存储过程的效率 目录 第1章 如何打开SQL Server Profile. 3 第2章 SQL Server Profile. ...

  10. 微信支付:redirect-uri参数错误 的解决办法

    redirect_url参数错误: 报这个错误,说明你的公众号后台授权设置有问题(一般有两处) 一:检查授权目录 答:支付授权目录是网站发起请求的页面所在目录,并且必须是能通过url地址访问的(与真实 ...