(状压) Brush (IV) (Light OJ 1018)

 

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个点, 求最少的直线将所有的点都覆盖
 
 
记忆化搜索:
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef unsigned long long LL;
#define met(a,b) (memset(a,b,sizeof(a)))
const int INF = 1e9+7;
const int maxn = 105;
const int MOD = 9973;

struct node
{
    int x, y;
} a[20];

int dp[(1<<17)], n;
int Line[20][20];
///Line[i][j] 代表与线段ij共线的点

int DFS(int sta)
{
    if(dp[sta]!=-1) return dp[sta];

    dp[sta] = INF;
    int cnt=0;
    for(int i=0; i<n; i++)
        if(sta&(1<<i)) cnt++;

    if(cnt==0) return dp[sta]=0;
    else if(cnt<=2) return dp[sta]=1;

    for(int i=0; i<n; i++)
    {
        if(sta&(1<<i))///第i个物品
        {
            for(int j=i+1; j<n; j++)
            {
                int w = (sta|Line[i][j]) - Line[i][j];
                dp[sta] = min(dp[sta], DFS(w)+1);
            }
            break;
            ///优化,只需找到sta中的一个点即可, Line[i][j]会将所有的i到i后面的点都遍历一遍的
        }
    }
    return dp[sta];
}

int main()
{
    int T, iCase = 1;
    scanf("%d", &T);

    while(T --)
    {
        int i, j, k, K;
        scanf("%d", &n);

        K = (1<<n)-1;
        met(dp, -1);
        met(Line, 0);
        for(i=0; i<n; i++)
        {
            scanf("%d%d", &a[i].x, &a[i].y);
            Line[i][i] = (1<<i);
        }

        for(i=0; i<n; i++)
        for(j=i+1; j<n; j++)
        for(k=0; k<n; k++)
        {   ///判断三点是否共线
            if( (a[i].x-a[k].x)*(a[i].y-a[j].y) == (a[i].x-a[j].x)*(a[i].y-a[k].y) )
                Line[i][j] += (1<<k);
        }

        printf("Case %d: %d\n", iCase++, DFS(K));
    }
    return 0;
}

 

先预处理出来每条线段,对每一个状态选择两个不在状态的点,然后画以两个点为端点的线,来进行状态转移

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef unsigned long long LL;
#define met(a,b) (memset(a,b,sizeof(a)))
const int INF = 1e9+7;
const int maxn = 105;
const int MOD = 9973;

struct node
{
    int x, y;
}a[20];

vector<int>G[(1<<17)];
int dp[(1<<17)], n;
int Line[20][20];
///Line[i][j] 代表与线段ij共线的点


int main()
{
    int T, iCase = 1, i, j;

    for(i=0; i<(1<<16); i++)
    for(j=0; j<16; j++)
    {
        if((i&(1<<j))==0)
         G[i].push_back(j);
    }

    scanf("%d", &T);

    while(T --)
    {
        int k, K;
        scanf("%d", &n);

        K = (1<<n)-1;
        met(dp, INF);
        met(Line, 0);
        for(i=0; i<n; i++)
        {
            scanf("%d%d", &a[i].x, &a[i].y);
            Line[i][i] = (1<<i);
        }

        for(i=0; i<n; i++)
        for(j=i+1; j<n; j++)
        for(k=0; k<n; k++)
        {   ///判断三点是否共线
            if( (a[i].x-a[k].x)*(a[i].y-a[j].y) == (a[i].x-a[j].x)*(a[i].y-a[k].y) )
                Line[i][j] += (1<<k);
        }

        dp[0] = 0;
        for(i=0; i<K; i++)
        {
            int len = G[i].size();
            int x=G[i][0], y;
            for(j=0; j<len; j++)
            {
                y = G[i][j];
                dp[i|Line[x][y]] = min(dp[i|Line[x][y]], dp[i]+1);
            }
        }

        printf("Case %d: %d\n", iCase++, dp[K]);
    }
    return 0;
}

/**

2

3
0 0
1 1
2 2

3
0 0
1 1
2 3

*/

 

 

转载于:https://www.cnblogs.com/YY56/p/5532470.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值