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 ...
随机推荐
- DELPHI出现无法加载dclite50.bpl的解决办法(转)
现象: Borland Integrated Translation Environment 加载出错 解决办法: 我的电脑--->(鼠标右键)属性--->高级--->(性能)设置- ...
- iOS 警告窗口
- (IBAction)buttonPressed:(id)sender { NSDate *date=self.datePicker.date; NSString *messag ...
- thinkphp3.2 分页方式汇总
//自定义分页 $page = $_GET['page'] ? $_GET['page'] : 1 ; $count = $this->Table("user")->c ...
- C++:FMC 错误
1.generated debug assertion -- File: docsingl.cpp Line: 215 MFC程序vs2008编译通过,运行时出错,无法打开,提示f:\dd\xxxx的 ...
- 删除ecshop云服务及授权关于官方等信息
一.删除[云服务中心] 删除/admin/cloud.php 删除/admin/templates/menu.htm中以下代码 Ajax.call('cloud.php?is_ajax=1>ac ...
- andriod 新建 Activity_ Form (详细设置)
参考: Starting Another Activity 去创建Activity New->Other->Android->Android Activity->BlankAc ...
- LA 3907 Puzzle
问题描述:先给你s个禁止串,求不包含禁止串的最长串,如果存在,打印字典序最大. 数据范围:s <= 1000, 禁止串长度不超过50. 分析:不匹配问题实际上等同于匹配问题.假设我们已经有满足条 ...
- Financial Management 分类: POJ 2015-06-11 10:51 12人阅读 评论(0) 收藏
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 164431 Accepted: ...
- python 中time.sleep没有作用
很简单的一个程序: # -*- coding: utf-8 -*- import MySQLdb,os,json,time #from wsgiref.simple_server import mak ...
- Android的消息处理机制Looper,Handler,Message
android的消息处理有三个核心类:Looper,Handler和Message.其实还有一个Message Queue(消息队列),但是MQ被封装到Looper里面了,我们不会直接与MQ打交道,因 ...