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

题目描述

传送门

题解

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

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值