UVA 11008 Antimatter Ray Clearcutting(状态压缩+记忆化搜索)

题意:

n个坐标上面分别有n棵树,每发射一次枪,可以把一条直线上面的树都消灭掉。问要消灭到m棵树,最少需要几枪。

解析:

这个题目由于树的数目很少,我们可以把树的状态压缩成一个整数,并依此来进行状态转移。dp[st]表示当前这个状态下面至少要开多少枪。
dp[st] = min(dp[st], dps(cur-cnt, new_st) + 1);
根据这个公式,进行状态转移。

AC代码

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = (1 << 16) + 100;
const int INF = 0x3f3f3f3f;
int dp[N];
struct Point {
    int x,y;
}p[20];
int n, m;
bool multi(Point a,Point b,Point c) {
    return (a.x - b.x) * (a.y - c.y) == (a.x - c.x) * (a.y - b.y);
}
int dps(int cur,int st) {
    if(dp[st] != INF) {
        return dp[st];
    }else if(cur <= 0) {
        return 0;
    }else if(cur == 1) {
        return dp[st] = 1;
    }
    for(int i = 0; i < n; i++) {
        if(!((1 << i) & st)) { //如果该点没有树
            continue;
        }
        for(int j = i+1; j < n; j++) {
            if(!((1 << j) & st)) {
                continue;
            }
            int tmp = st ,cnt = 0;
            for(int k = i; k < n; k++) {
                if((1 << k) & st && multi(p[i],p[j],p[k])) {
                    tmp -= (1 << k);
                    cnt++;
                }
            }
            dp[st] = min(dp[st], dps(cur-cnt,tmp) + 1);
        }
    }
    return dp[st];
}
int main() {
    int T, x, y, cas = 1;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++) {
            scanf("%d%d",&x,&y);
            p[i].x = x;
            p[i].y = y;
        }
        memset(dp,INF,sizeof(dp));
        printf("Case #%d:\n", cas++) ;
        printf("%d\n", dps(m, (1 << n)-1)) ;
        if(T) puts("") ;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值