[LightOJ 1018] Brush (IV) (状压DP)

Description

Mubashwir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found an old toothbrush in his room. Since the dusts are scattered everywhere, he is a bit confused what to do. So, he called Shakib. Shkib said that, 'Use the brush recursively and clean all the dust, I am cleaning my dust in this way!'

So, Mubashwir got a bit confused, because it's just a tooth brush. So, he will move the brush in a straight line and remove all the dust. Assume that the tooth brush only removes the dusts which lie on the line. But since he has a tooth brush so, he can move the brush in any direction. So, he counts a move as driving the tooth brush in a straight line and removing the dusts in the line.

Now he wants to find the maximum number of moves to remove all dusts. You can assume that dusts are defined as 2D points, and if the brush touches a point, it's cleaned. Since he already had a contest, his head is messy. That's why he wants your help.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with a blank line. The next line contains three integers N (1 ≤ N ≤ 16)N means that there are N dust points. Each of the next N lines will contain two integers xi yi denoting the coordinate of a dust unit. You can assume that (-1000 ≤ xi, yi ≤ 1000) and all points are distinct.

Output

For each case print the case number and the minimum number of moves.

Sample Input

2

 

3

0 0

1 1

2 2

 

3

0 0

1 1

2 3

Sample Output

Case 1: 1

Case 2: 2


平面上若干个点,问最少需要几条直线能够把他们全部连起来。我原来的做法是贪心,先选中一个点,然后过这点找一条直线能够链接最多的点,但是贪得不对。所以只能DP搞。只有16个点,所以可以状压。用一个数每一位表示一个点是否已经被连上了。先预处理一下每两个点连上之后能顺便把那些点连上了。从最开始都连上的状态,每次找两个点,去掉这条直线上的所有点,用记忆化搜索搜回去。边界是都没连上的状态为0, 只有一个点的状态为1。以下是一段蠢得不行的代码。


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int minn(int a,int b){return a<b? a: b;}
struct Point
{
	int x,y;
};
bool judge(Point &i, Point &j, Point &k){return (j.y-i.y)*(k.x-i.x)==(k.y-i.y)*(j.x-i.x);}
int N;
Point inpt[20];
int stat[20][20];
int dp[(1<<16)+100];

int DP(int);
int main()
{
	int T;
	scanf("%d", &T);
	for(int ck=1; ck<=T; ck++)
	{
		scanf("%d", &N);
		memset(stat, 0, sizeof(stat));
		for(int i=0; i<(1<<N); i++) dp[i]=9;
		for(int i=0; i<N; i++) scanf("%d%d", &inpt[i].x, &inpt[i].y);
		for(int i=0; i<N; i++)
		{
			for(int j=i+1; j<N; j++)
			{
				int tem=(1<<j)|(1<<i);
				for(int k=0; k<N; k++)
				{
					if(k==i||k==j) continue;
					if(judge(inpt[i],inpt[j],inpt[k])) tem|=(1<<k);
				}
				stat[i][j]=tem;
			}
		}
		printf("Case %d: %d\n", ck, DP((1<<N)-1));
	}
	return 0;
}

int DP(int sta)
{
	if(dp[sta]<9) return dp[sta];
	int bcnt=0;
	for(int i=0; i<N; i++) if(sta&(1<<i)) bcnt++;
	if(bcnt==0){return dp[sta]=0;}
	if(bcnt<=2){return dp[sta]=1;}
	for(int i=0; i<N; i++)
	{
		if((sta&(1<<i))==0) continue;
		for(int j=i+1; j<N; j++)
		{
			if((sta&(1<<j))==0) continue;
			dp[sta]=minn(dp[sta],DP(sta^(sta&stat[i][j]))+1);//原来状态是sta^stat[i][j],结果把已经连上的又异或回去了,爆栈了
		}
		break;//找到直线一个端点,枚举另一个端点转移就好,因为直线是没有顺序的
	}
	return dp[sta];
}



你可能感兴趣的:([LightOJ 1018] Brush (IV) (状压DP))