您的位置:首页 > 其它

poj 3304 Segments(计算几何)

2013-08-19 16:29 417 查看
Segments

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 8246Accepted: 2496
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 integern ≤ 100 showing the number of segments. After that,
n lines containing four real numbersx1 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 numbersa 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!

Source
Amirkabir University of Technology Local Contest 2006

题意:求是否存在这样的一条直线,直线上有一点被给出的所有直线在这条直线上的所有投影覆盖
题解:这等价于求一条过所有线段的直线是否存在,这也等价于枚举所有端点,是否能构成一条直线过所有线段

#include<stdio.h>
#include<math.h>
#define eps 1e-8
struct point{
double x,y;
}p[203];
int n;
double cross(struct point p1,struct point p2,struct point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
int judge(struct point a,struct point b)
{
int i;

if(sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))<eps) return 0;
for(i=0;i<n;i+=2)
{
if(cross(a,b,p[i])*cross(a,b,p[i+1])>eps) return 0;
}

return 1;
}
int jerry()
{
int i,j;

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(judge(p[i],p[j])) return 1;
}

return 0;
}
int main()
{
int t,i;

scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
n=n*2;
for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
if(jerry()) printf("Yes!\n");
else printf("No!\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: