Segments_poj3304_计算几何

来源:互联网 发布:温湿度数据采集器 编辑:程序博客网 时间:2024/05/18 00:02

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;}
0 0
原创粉丝点击