欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UPC - 8839: Parallel Lines(DFS暴力)

程序员文章站 2022-07-07 18:55:41
...

 

贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。

8839: Parallel Lines

时间限制: 10 Sec  内存限制: 128 MB
提交: 120  解决: 22
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Given an even number of distinct planar points, consider coupling all of the points into pairs.
All the possible couplings are to be considered as long as all the given points are coupled to one and only one other point.
When lines are drawn connecting the two points of all the coupled point pairs, some of the drawn lines can be parallel to some others. Your task is to find the maximum number of parallel line pairs considering all the possible couplings of the points. 
For the case given in the first sample input with four points, there are three patterns of point couplings as shown in Figure B.1. The numbers of parallel line pairs are 0, 0, and 1, from the left. So the maximum is 1.

UPC - 8839: Parallel Lines(DFS暴力)

Figure B.1. All three possible couplings for Sample Input 1
For the case given in the second sample input with eight points, the points can be coupled as shown in Figure B.2. With such a point pairing, all four lines are parallel to one another. In other words, the six line pairs (L1, L2), (L1, L3), (L1, L4), (L2, L3), (L2, L4) and (L3, L4) are parallel. So the maximum number of parallel line pairs, in this case, is 6.

 

输入

The input consists of a single test case of the following format.
m
x1 y1
.
.
.
xm ym

UPC - 8839: Parallel Lines(DFS暴力)

Figure B.2. Maximizing the number of parallel line pairs for Sample Input 2
The first line contains an even integer m, which is the number of points (2 ≤ m ≤ 16). Each of the following m lines gives the coordinates of a point. Integers xi and yi (−1000 ≤ xi ≤ 1000,−1000 ≤ yi ≤ 1000) in the i-th line of them give the x- and y-coordinates, respectively, of the i-th point.
The positions of points are all different, that is, xi ≠ xj or yi ≠ yj holds for all i ≠ j. Furthermore, No three points lie on a single line.

输出

Output the maximum possible number of parallel line pairs stated above, in one line.

 

样例输入

4
0 0
1 1
0 2
2 4

 

样例输出

1

 

来源/分类

ICPC 2017 Japan Tsukuba 

 

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define rep(i,j,k) for(int i=j;i<k;i++)
#define per(i,j,k) for(int i=j;i<=j;i++) 
const int maxn = 20;
typedef long long ll;
int a[maxn],b[maxn];
int vis[maxn];
int n,ans;
vector <pair<int ,int> >  que;
void get_max()
{
    int sum=0;
    rep(i,0,que.size())
    {
        int x1= a[que[i].first]-a[que[i].second];
        int y1= b[que[i].first]-b[que[i].second];
        rep(j,0,que.size()) 
        {
            if(i==j) continue;
            int x2 = a[que[j].first]-a[que[j].second];
            int y2 = b[que[j].first]-b[que[j].second];
            if(x1*y2==y1*x2) sum++;
        }
    }
    ans = max(ans,sum/2);
}
void dfs(int cur)
{
     if(cur==n){
        get_max();
        return;
     }
     if(vis[cur]) return dfs(cur+1);   //用过的点将继续找 
     rep(i,0,n)
     {
            if(!vis[i]&&i!=cur) 
            {
                que.push_back(make_pair(cur,i)); 
                vis[i]= vis[cur] =1;              //每次找两个可行点连成一条线       
                dfs(cur+1);
                que.pop_back(); 
                vis[i]=vis[cur]=0;               //遍历所有
            }
     }
}

int main(void)
{
    IO
    while(cin>>n)
    {
    memset(vis,0,sizeof(vis)); 
    ans=0;
    rep(i,0,n)   cin>>a[i]>>b[i];
    dfs(0);
    cout<<ans<<endl;
    }
}