LightOJ 1018 - Brush (IV)(记忆化搜索)

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

代码

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;
const int maxn = 17;
const int maxs = 1<<17;
const int inf = 0x3f3f3f3f;

int N, S[maxn][maxn], X[maxn], Y[maxn], D[maxs];

bool judge (int x1, int y1, int x2, int y2) {
    return x1 * y2 == y1 * x2;
}

void init () {
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
        scanf("%d%d", &X[i], &Y[i]);

    memset(D, -1, sizeof(D));
    memset(S, 0, sizeof(S));
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N; j++) {
            int& s = S[i][j];
            s = 0;
            for (int k = 0; k < N; k++) {
                if (!judge(X[k]-X[i], Y[k]-Y[i], X[j]-X[i], Y[j]-Y[i]))
                    s |= (1<<k);
            }
        }
    }
}

int bitcount(int x) { return x == 0 ? 0 : bitcount(x>>1) + (x&1); }

int dfs (int s) {
    if (D[s] != -1) return D[s];
    int c = bitcount(s);
    if (c == 0) return D[s] = 0;
    if (c <= 2) return D[s] = 1;
    int& ans = D[s];
    ans = inf;

    for (int i = 0; i < N; i++) if (s&(1<<i)) {
        for (int j = i+1; j < N; j++) if (s&(1<<j))
            ans = min(ans, dfs(s&S[i][j]) + 1);
        break;
    }
    return ans;
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        init();
        printf("Case %d: %d\n", kcas, dfs((1<<N)-1));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值