Aizu —— 1379 Parallel Lines(暴力搜索)

Problem B

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.

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,L2), (L1,L3)(L1,L3), (L1,L4)(L1,L4), (L2,L3)(L2,L3), (L2,L4)(L2,L4) and (L3,L4)(L3,L4) are parallel. So the maximum number of parallel line pairs, in this case, is 6.

Input

The input consists of a single test case of the following format.

mm
x1x1 y1y1
...
xmxm ymym

Figure B.2. Maximizing the number of parallel line pairs for Sample Input 2

The first line contains an even integer mm, which is the number of points (2≤m≤162≤m≤16). Each of the following mm lines gives the coordinates of a point. Integers xixi and yiyi (−1000≤xi≤1000,−1000≤yi≤1000−1000≤xi≤1000,−1000≤yi≤1000) in the ii-th line of them give the xx- and yy-coordinates, respectively, of the ii-th point.

The positions of points are all different, that is, xi≠xjxi≠xj or yi≠yjyi≠yj holds for all i≠ji≠j. Furthermore, No three points lie on a single line.

Output

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

Sample Input 1

4
0 0
1 1
0 2
2 4

Sample Output 1

1

Sample Input 2

8
0 0
0 5
2 2
2 7
3 -2
5 0
4 -2
8 2

Sample Output 2

6

Sample Input 3

6
0 0
0 5
3 -2
3 5
5 0
5 7

Sample Output 3

3

Sample Input 4

2
-1000 1000
1000 -1000

Sample Output 4

0

Sample Input 5

16
327 449
-509 761
-553 515
360 948
147 877
-694 468
241 320
463 -753
-206 -991
473 -738
-156 -916
-215 54
-112 -476
-452 780
-18 -335
-146 77

Sample Output 5

12

这个题当时想暴力,没敢写,主要是多种斜率存在情况下,不会写了,原来是搜索啊,果然搜索还是很牛逼!!

题意:坐标轴中,给出偶数个点,任意两点相连,问互相平行的线最多有多少。

题解:不一定是一种斜率就是最多,可以有多种不同~~~坑~~

上代码:

#include <iostream>
using namespace std;
const int MAX = 21;
struct Point{
	int x,y;
}point[MAX];//存点坐标
struct Vector{
	int a,b;
}vector[MAX];//存直线向量
int n,maxx,cnt;
bool vis[MAX];
void jisuan(){
	int ans=0;
	for (int i = 0; i < cnt;i++){
		for (int j = i+1; j < cnt;j++){
			if(vector[i].a*vector[j].b==vector[i].b*vector[j].a) ans++;//向量判断是否平行
		}
	}
	maxx=max(maxx,ans);
	return;
}
void dfs(int index){
	while(vis[index]) index++;//找过的点不找了,很重要,不然会T而且会错
	if(index>=n){
		jisuan();
		return;
	}
	vis[index]=true;
	for (int i =  0; i < n;i++){
		if(!vis[i]&&index!=i){//没用过且不是同一个点
			vis[i]=1;
			int xx=point[i].x-point[index].x;//计算直线向量
			int yy=point[i].y-point[index].y;
			vector[cnt].a=xx;
			vector[cnt++].b=yy;
			dfs(index+1);
			cnt--;
			vis[i]=false;
		}
	}
	vis[index]=0;//这个别忘了回溯
	return;
}
int main(){
	cin >> n;
	for (int i = 0;i < n;i++){
		cin >> point[i].x >> point[i].y;
	}
	maxx=0;
	dfs(0);
	cout << maxx << endl;
	return 0;
} 

果然水题,只是想不到怎样搜~~~加强训练啊~~ 

 

全部评论

相关推荐

今天周一休息,突发奇想写一篇阶段总结。如题,我已经去了一个和Java彻底毫无关联的行业。曾经我以为自己能在计算机行业发光发热,没想到刚入行一年多就当了逃兵。从最开始的热爱到现在一看到代码就厌恶,不知道自己经历了什么。所以我去干什么了?答案是:在成都当了租房销售。上班那会压力大了就念叨着去干租房中介,但是一直下不去这个决心,想着自己学了四年多的计算机知识,终究还是不甘心。终于在某一天准备八股文的时候,看着无数篇和工作内容关系不大的理论知识,那一刻下定决心,决定尝试一下销售行业,也算是给自己一个交代。后面阴差阳错的投了成都自如去当租房管家,没想到面试很顺利,在当天一百多个面试的人里面,我成为了为数不多通过的几个幸运儿之一。目前已经培训通过,正式入职,也开了单,也有压力但是每天过得很开心,真心喜欢那种和人交流的感觉,哪怕是最后没有选择找我租房。说这些也是想告诉那些大三,大四正在找Java实习而焦虑的同学:你们现在还年轻,选择很多,容错率也很高,可以尽情去尝试自己喜欢的行业和工作。不用因为某一次的面试没通过或者简历石沉大海而焦虑,更不用因为身边人都在挤编程的独木桥就强迫自己跟风。也算是自己的碎碎念吧,也希望自己能在新的领域取得一点小成就。也祝牛油工作顺利!
沉淀小子:干啥都不丢人啊,生存是必须要的,销售很考验一个人综合素质能力的,好的销售人脉和资源可不比写字楼的白领差啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务