light oj 1018 - Brush (IV)(状压DP求直线容纳所有点的最少数量)

该博客讨论了如何使用动态规划和状态压缩技术解决在二维平面上用最少的直线覆盖给定点集的问题。通过预处理线段并利用二进制位运算优化,博主给出了一个解决方案,用于找到覆盖所有点所需的最小线段数。
摘要由CSDN通过智能技术生成

1018 - Brush (IV)
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



目描述:

  有n个点,问最少用几条无线长的线段把这n个点全部覆盖起来?
解题思路:
  n最大是16,就把所有的线段预处理出来用二进制保存下来,对每个状态枚举每条线段,然后就TLE,TEL,TEL。
  其实应该换一种枚举方式,先预处理出来每条线段,对每一个状态选择两个不在状态的点,然后画以两个点为端点的线,来进行状态转移。(因为是一个一个遍历所以可以遍布所有情况)。这个题目最重要的优化在于如何挑选不在状态的两个点,测试实力比较多,我们可以预处理出来每个状态未覆盖的点。

因为数量小可以用状压,。。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int N = 17;
const int inf = 0x3f3f3f3f;
int dp[1<<N], line[N][N];
vector<int>G[1<<N];
struct node
{
    int x, y;
}p[N];
int judge(int a,int b,int c)
{
    return (p[b].y-p[a].y)*(p[c].x-p[b].x)==(p[c].y-p[b].y)*(p[b].x-p[a].x);
}


int main()
{
    for(int i=0;i<(1<<N);i++)
    {
        G[i].clear();
    }


    for(int i=0;i<(1<<N);i++)
    {
        for(int j=0;j<N;j++)
        {
            if((i&(1<<j))==0)
            {
                G[i].push_back(j);
            }
        }
    }
    int t, n, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        int num=(1<<n);
        memset(line,0,sizeof(line));
        for(int i=0;i<n;i++)
        {
            scanf("%d %d",&p[i].x,&p[i].y);
            line[i][i]=(1<<i);
        }


        memset(dp,inf,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                for(int k=0;k<n;k++)
                {
                    if(judge(i,j,k))
                        line[i][j]|=(1<<k);
                }
            }
        }
        dp[0]=0;
        for(int i=0;i<num;i++)
        {
            int x=G[i][0];
            for(int j=0;j<G[i].size();j++)
            {
                int y=G[i][j];
                dp[i|line[x][y]]=min(dp[i|line[x][y]],dp[i]+1);
            }
        }
        printf("Case %d: %d\n",ncase++,dp[num-1]);
    }
    return 0;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值