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.
Analysis
问题可以转换成求是否存在一条直线能穿过所有给出的线段
不难想到这条直线在一定范围内平移旋转仍能保持其原性质(穿过所有的线段),于是可以将直线平移旋转,使其穿过某些线段的端点
∵两点确定一条直线
∴我们可以枚举两个端点作直线,再枚举线段利用叉积判断是否被直线穿过
poj的pascal编译莫名爆了,可怜某媳妇一秒钟,c++大法好
Code
#include <stdio.h>
using namespace std;
struct point
{
double x,y;
}p[201];
double cros(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
int main()
{
int t,n;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
bool flag,ans=false;
int cnt=0;
for (int i=1;i<=n;i++)
{
++cnt;scanf("%lf%lf",&p[cnt].x,&p[cnt].y);
++cnt;scanf("%lf%lf",&p[cnt].x,&p[cnt].y);
}
for (int i=1;i<=cnt-1&&!ans;i++)
for (int j=i+1;j<=cnt&&!ans;j++)
{
if (p[i].x==p[j].x&&p[i].y==p[j].y)
continue;
flag=false;
for (int k=1;k<=cnt;k+=2)
if (cros(p[i],p[j],p[k])*cros(p[i],p[j],p[k+1])>0)
{
flag=true;
break;
}
if (!flag)
{
ans=true;
printf("Yes!\n");
}
}
if (!ans)
printf("No!\n");
}
return 0;
}

本文探讨了一道关于二维空间中线段投影的问题:如何判断是否存在一条直线,使得所有线段在其上的投影至少有一点交集。通过分析问题特性,提出了解决方案,并提供了具体的算法实现。
2639

被折叠的 条评论
为什么被折叠?



