lightoj 1018 brush(四)(状压DP)

来源:互联网 发布:ksvd算法去噪 编辑:程序博客网 时间:2024/05/17 20:34
1018 - Brush (IV)
PDF (English)StatisticsForum
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 as2D 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 nextN 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 20int 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;}



0 0
原创粉丝点击