lightoj 1018 - Brush (IV) 【状压dp】

题目链接:lightoj 1018 - Brush (IV)

1018 - Brush (IV)
PDF (English) Statistics Forum
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

PROBLEM SETTER: AKM MUBASHWIR ALAM
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)

题意:n个点,用最少的直线覆盖。

思路:状态压缩dp,枚举每条直接。不过,很卡时间。。。在记忆化的时候胡乱break一下就过了。。。先挂上吧。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#define ll o<<1
#define rr o<<1|1
#define fi first
#define se second
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int MAXN = 3*1e5 + 10;
int dp[1<<16];
int state[16][16];
int x[16], y[16];
bool judge(int i, int j, int k) {
    return (x[k] - x[i]) * (y[j] - y[i]) == (y[k] - y[i]) * (x[j] - x[i]);
}
int n;
int DFS(int s) {
    if(dp[s] != INF) return dp[s];
    for(int i = 0; i < n; i++) {
        if((s & (1<<i)) == 0) continue;
        for(int j = i+1; j < n; j++) {
            if((s & (1<<j)) == 0) continue;
            dp[s] = min(dp[s], DFS(s^(s&state[i][j])) + 1);
        }
        break;
    }
    return dp[s];
}
int main()
{
    int t, kcase = 1;
    scanf("%d", &t);
    while(t-- ) {
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &x[i], &y[i]);
        }
        for(int i = 0; i < (1<<n); i++) {
            dp[i] = INF;
        }
        for(int i = 0; i < n; i++) {
            dp[(1<<i)] = 1;
        }
        dp[0] = 0;
        for(int i = 0; i < n; i++) {
            for(int j = i+1; j < n; j++) {
                int s = 0; s |= (1 << i); s |= (1 << j);
                dp[s] = 1;
                for(int k = 0; k < n; k++) {
                    if(k == i || k == j) continue;
                    if(judge(i, j, k)) {
                        s |= (1 << k);
                    }
                }
                dp[s] = 1; state[i][j] = s;
            }
        }
        printf("Case %d: %d\n", kcase++, DFS((1<<n)-1));
    }
    return 0;
}

你可能感兴趣的:(lightoj 1018 - Brush (IV) 【状压dp】)