[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];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值