LightOJ 1018 Brush (IV)(状态压缩DP)

给定平面上不共线的点,求最少使用多少条直线能覆盖所有点。采用状态压缩动态规划方法解决,预处理点对之间的直线,并通过位运算进行状态转移。
摘要由CSDN通过智能技术生成

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

题意:

给出n个点的坐标,问最少需要多少条直线可以覆盖所有的点。

分析:

因为n小于等于16,所以很直观会想到状态压缩。
这道题关键是要预处理一个line[i][j] 表示点i和j所在直线 ,可以覆盖的点。
然后就是状态定义dp[s] s表示没有被覆盖的点,dp[s]表示要把s中的点全部覆盖掉 所使用的最少直线数。
转移方程:
dp[s]=min(dp[s],dp[s & (~line[i][j])+1);
边界条件:
dp[0]=0;
dp[1]=dp[2]=1;

另外子尧学长的题解写的好细致,代码里还有注释!一看就能懂得那种大赞!
用平行向量来判断在同一条直线上也使得代码变得好写!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int x[20],y[20];
int line[20][20];
int dp[1<<16];
int n;
void init(){
    memset(line,0,sizeof line);
    memset(dp,INF,sizeof dp);
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            int dx=x[j]-x[i],dy=y[j]-y[i];
            for(int k=0;k<n;k++){
                int dx2=x[k]-x[i],dy2=y[k]-y[i];
                if(dx*dy2==dy*dx2){
                    line[i][j]|=(1<<k);
                }
            }
            line[j][i]=line[i][j];
        }
    }
//  cout<<line[2][1]<<endl;
}
int dfs(int s){
    if(dp[s]!=INF) return dp[s];
    int num=__builtin_popcount(s);
    if(num==0) return dp[s]=0;
    if(num<=2) return dp[s]=1;
    int i=0;
    while((s & (1<<i)) == 0) i++;
    for(int j=i+1;j<n;j++){
        if(s & (1<<j)){
            dp[s]=min(dp[s],dfs(s & (~line[i][j]))+1);
        }
    }
    return dp[s];
}
int main(){
    freopen("in.txt","r",stdin);
    int T,cas=0;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d%d",&x[i],&y[i]);
        }
        init();
        printf("Case %d: %d\n",++cas,dfs((1<<n)-1));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值