lightoj 1018 brush(四)(状压DP)

1018 - Brush (IV)
Time Limit: 2 second(s)Memory Limit: 32 MB

Mubashwir returned home from the contest and got angry afterseeing his room dusty. Who likes to see a dusty room after a brain stormingprogramming contest? After checking a bit he found an old toothbrush in hisroom. Since the dusts are scattered everywhere, he is a bit confused what todo. So, he called Shakib. Shkib said that, 'Use the brush recursively and cleanall the dust, I am cleaning my dust in this way!'

So, Mubashwir got a bit confused, because it's just a toothbrush. 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. Butsince he has a tooth brush so, he can move the brush in any direction. So, hecounts a move as driving the tooth brush in a straight line and removing thedusts in the line.

Now he wants to find the maximum number of moves to removeall dusts. You can assume that dusts are defined as 2D points, and ifthe brush touches a point, it's cleaned. Since he already had a contest, hishead 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 containsthree integers N (1 ≤ N ≤ 16). N means that there areN dust points. Each of the next N lines will contain two integersxi yi denoting the coordinate of a dust unit. Youcan assume that (-1000 ≤ xi, yi ≤ 1000)and all points are distinct.


题意:

给你n(<=16)个点,问你最少可以用几条直线可以将这n个点覆盖。

思路:

先两个点两个点的枚举直线,将这条直线上存在的点数全部都记录下来。

p[i][j]表示点i,j所在直线存在点的状态。

将所有的点的状态存起来用s来表示.

那么dp[s] = min(dp[s], dp[s'] + 1);

s' 为去掉一条直线后的状态。

/***********************************************
 * Author: fisty
 * Created Time: 2015/6/27 11:43:45
 * File Name   : 1018.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 20
int n;
int p[MAX_N][MAX_N];
int dp[1<<16];
struct point{
    int x, y;
}line[MAX_N];

bool on_line(point a, point b, point c){
    return (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x) == 0;
}
void init(){
    Memset(p, 0);
    FOR(i, 0, n){
        FOR(j, i+1, n){
            FOR(k, 0, n){
                if(on_line(line[i], line[j], line[k]))
                    p[i][j] += (1<<k);
            }
        }
    }
}
int dfs(int s){
    if(dp[s] != INF) return dp[s];
    int cnt = 0;
    FOR(i, 0, n) if((1<<i)&s) cnt++;
    if(cnt == 0) return dp[s] = 0;
    if(cnt <= 2) return dp[s] = 1;
    
    FOR(i, 0, n){
        if((1<<i)&s){
            FOR(j, i+1, n){
                if((1<<j)&s){
                    dp[s] = min(dp[s], dfs(s-(s&p[i][j])) + 1);
                }
            }
            break;
        }
    }
    
    return dp[s];
}
int main() {
    //freopen("in.cpp", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    int ca = 1;
    while(t--){
        cin >> n;
        Memset(line, 0);
        FOR(i, 0, n){
            cin >> line[i].x >> line[i].y;
        }
        init();
        fill(dp, dp + (1<<16), INF);
        cout << "Case " << ca++ << ": ";
        cout << dfs((1 << n) - 1) << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值