poj 3304线段与直线相交
http://poj.org/problem?id=3304
| 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 (x1, y1) and (x2, y2) 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线段与直线相交的更多相关文章
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- POJ 3304 Segments (判断直线与线段相交)
题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...
- POJ 1039 Pipe【经典线段与直线相交】
链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ 3304 Segments 判断直线和线段相交
POJ 3304 Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...
- POJ 3304 Segments (直线与线段是否相交)
题目链接 题意 : 能否找出一条直线使得所有给定的线段在该直线上的投影有一个公共点. 思路 : 假设存在一条直线a使得所有线段在该直线上的投影有公共点,则必存在一条垂直于直线a的直线b,直线b与所有线 ...
- POJ 3304 Segments (直线和线段相交判断)
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 2316 Descript ...
- poj 3304 Segments 线段与直线相交
Segments Time Limit: 1000MS Memory Limit: 65536K Description Given n segments in the two dim ...
- poj 1269 Intersecting Lines(直线相交)
Intersecting Lines Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8637 Accepted: 391 ...
- POJ 3304 segments 线段和直线相交
Segments Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14178 Accepted: 4521 Descrip ...
随机推荐
- :has(selector) 匹配含有选择器所匹配的元素的元素
描述: 给所有包含 p 元素的 div 元素添加一个 text 类 HTML 代码: <div><p>Hello</p></div> <div&g ...
- scala抽象类抽象字段
package com.test.scala.test /** * 抽象类学习,定义abstact关键字 */ abstract class AbstractClass { val id:Int;// ...
- iOS发布条款检查表
序号 分类 条款编号 条款 案例 1 功能 2.1 崩溃的程序将会被拒绝 2 2.2 有错误的程序将会被拒绝 点击版本升级无反应/点击版本升级,在线版本和当前版本都是2.0.3 3 2.3 跟开发者宣 ...
- UTF8转码, 考虑利用app转好再传, CC2541转太麻烦了...
- ubuntu 修改终端命令显示的颜色
转于 http://www.blogbus.com/riusksk-logs/62891140.html 修改当前用户 gedit ~/.bashrc 在最后一行下面添加这行 PS1='${debi ...
- Java中多线程使用匿名内部类的方式进行创建3种方式
/* * 匿名内部类的格式: */ public class ThreadDemo { public static void main(String[] args) { // 继承thread类实现多 ...
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.4.Tabs控件
之前,我们已经介绍了 jQuery UI 库,CSS 框架.下面,我们将学习这些有增强可视化效果,高度可配置的用户交互组件. Tab 的特性是,点击 tab 后,会高亮该 tab,并显示他的关联con ...
- Java常用jar包用途
Java常用jar包用途: USAGE INDEX JAR NAME USAGE 1 ASM asm-2.2.3.jar ASM字节码库 2 ASM asm-commons-2.2.3.jar ASM ...
- 直接在Chrome里抓取数据
一个小测试发现可以自动做题,于是想通过脚本的方式看能不能获取相应的题库,刚好可以学习一下JS异步操作.花了一天时间,总算跑顺利了,遇到了不少坑.记录下来分享. 1.JS如何顺序执行 JS有强大的异步操 ...
- java 数组基本操作(一维)
1.数组的声明: 数组类型 数组名[] 2.数组的表示方法 想使用数组中的值,可以使用索引来实现,数组是从0开始的,使用时格式为:数组名[i],比如 a[1],代表第二个值 在数组中要使用数组的长度 ...