[BZOJ1610][Usaco2008 Feb]Line连线游戏(计算几何)

题目描述

传送门

题解

两两枚举点求直线
相同斜率的只能画一条
注意特判斜率不存在

代码

#include
#include
#include
#include
#include
using namespace std;
#define N 40005

const double inf=1e9;
const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Point
{
    double x,y;
    Point(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
};

int n,cnt,ans;
double x,y,k[N];
Point pt[N];

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;++i)
    {
        scanf("%lf%lf",&x,&y);
        pt[i]=Point(x,y);
    }
    for (int i=1;i<=n;++i)
        for (int j=i+1;j<=n;++j)
        {
            Point A=pt[i],B=pt[j];
            if (A.x==B.x) k[++cnt]=inf;
            else k[++cnt]=(A.y-B.y)/(A.x-B.x);
        }
    sort(k+1,k+cnt+1);k[0]=-inf;
    for (int i=1;i<=cnt;++i)
        if (dcmp(k[i]-k[i-1])!=0) ++ans;
    printf("%d\n",ans);
}

你可能感兴趣的:(题解,计算几何)