Game of Lines(POJ-3668)

本文介绍了一道经典的算法题目,即如何通过给定的多个点来找出最多可以绘制多少条互不平行的直线,并提供了一个使用C++实现的具体算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i has the integer coordinates Xi and Yi (-1,000 ≤ Xi ≤ 1,000; -1,000 ≤ Yi ≤ 1,000).
Bessie can score a point in the game by picking two of the dots and drawing a straight line between them; however, she is not allowed to draw a line if she has already drawn another line that is parallel to that line. Bessie would like to know her chances of winning, so she has asked you to help find the maximum score she can obtain.

Input

Line 1: A single integer: N

Lines 2..N+1: Line i+1 describes lattice point i with two space-separated integers: Xi and Yi.

Output

Line 1: A single integer representing the maximal number of lines Bessie can draw, no two of which are parallel.

Sample Input

4

-1 1
-2 0
0 0
1 1

Sample Output

4

题意:给出n个点的坐标,求最多能确定多少个互不平行的直线。

思路:用atan()函数求出所有点的斜率,排序后进行判断即可。

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1050
#define MOD 123
#define E 1e-6
using namespace std;
struct Point{
    double x;
    double y;
}point[N];
double a[N*N];
double Calculate(Point p1, Point p2)
{
    Point p;
    p.x=p2.x-p1.x;
    p.y=p2.y-p1.y;

    if(fabs(p.x)<E)//斜率不存在
        return 1;

    double temp=atan(p.y/p.x);
    if(temp<0)//注意斜率的范围
        return 1+temp;
    else
        return temp;
}

int main()
{
    int n;
    scanf("%d",&n);

    for(int i=0;i<n;i++)
        scanf("%lf%lf",&point[i].x,&point[i].y);

    int k=0;
    for(int i=0;i<n;i++)
        for(int j=i+1;j<n;j++)
        {
            a[k]=Calculate(point[i],point[j]);
            k++;
        }

    sort(a,a+k);

    int ans=1;
    for(int i=1;i<k;i++)
        if(a[i]!=a[i-1])
            ans++;

    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值