您的位置:首页 > 其它

lightoj1018 - Brush (IV)【状压dp】

2016-04-12 16:59 387 查看
[align=center]1018 - Brush (IV)[/align]



 
  
PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB
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

Output for Sample Input

2

 

3

0 0

1 1

2 2

 

3

0 0

1 1

2 3

Case 1: 1

Case 2: 2

 

刚开始想的是贪心每次选出能覆盖点数最多的直线然后删除该直线上的点然后继续寻找下一次,感觉这样的到的就是最优解然后写好交了就wa了后来一想当选择的时候有多条直线覆盖的点数相同应该选取那条直线才能得到最优解?然后看了别人的思路状压dp然后就果断用for循环写了一个状压dp然后时间复杂度o(n*n*n+2^n*n*n)果断TLE了然后就记忆化搜索,之后还是TLE,然后就看了别人的思路加了两个剪枝。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=18;
struct Node{
int x,y;
}A[maxn];
int dp[1<<maxn];
bool judge(Node a,Node b,Node c){
return ((c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y))==0;
}
int line[maxn][maxn],n;
int dfs(int state){
if(state==((1<<n)-1))return dp[state]=0;//点已经全部加上
if(dp[state]==-1)dp[state]=inf;
else return dp[state];
int i,j;
bool sign=false;
for(i=0;i<n;++i){
if(state&(1<<i))continue;//剪枝当该点已经出现在已加上的点集中
for(j=i+1;j<n;++j){
if(state&(1<<j))continue;//同上不加此剪枝也可以过
sign=true;
int s=state|line[i][j];
dp[state]=min(dfs(s)+1,dp[state]);
}
break;
}
if(i>=n)return dp[state]=inf;//剩下的边都有点已经加在了点集中
if(!sign)return dp[state]=1;//剩下的边中每条边中最多只有一个点不在以加入的边集中
return dp[state];
}
int main()
{
int i,j,k,t,test=1,s;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<n;++i){
scanf("%d%d",&A[i].x,&A[i].y);
}
memset(line,0,sizeof(line));
for(i=0;i<n;++i){
for(j=i+1;j<n;++j){
for(k=0;k<n;++k){
if(judge(A[i],A[j],A[k]))
line[i][j]|=(1<<k);//枚举出共线的个数
}
}
}
memset(dp,-1,sizeof(dp));
printf("Case %d: %d\n",test++,dfs(0));//逆向搜索从没有加一条边开始
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lightoj1018 - Brush