HDU-4462 Scaring the Birds 状态压缩枚举

题意:一个裸的枚举题,告诉你M个点,问这M个点能否覆盖其他非M点。做的时候时间复杂度计算错误。。

代码如下:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int N, M;
int r[15];
int x[15], y[15];
char mp[55][55];
char tp[55][55];
const int INF = 0x3f3f3f3f;

bool judge(int x, int y) {
    if (x >= 1 && x <= N && y >= 1 && y <= N) return true;
    return false;
}

int gao(char hav[]) {
    int cnt = 0, tot = 0;
    memcpy(tp, mp, sizeof (mp));
    for (int i = 0; i < M; ++i) {
        if (!hav[i] || !r[i]) continue;
        ++cnt;
        for (int j = -r[i]; j <= r[i]; ++j) {
            for (int k = -r[i]; k <= r[i]; ++k) {
                int xx = x[i] + j, yy = y[i] + k;
                if (!judge(xx, yy)) continue;
                if (abs(j) + abs(k) <= r[i] && !tp[xx][yy]) {
                    ++tot, tp[xx][yy] = 1;
                }
            }
        }
    }
    return (tot + M == N*N) ? cnt : INF;
}

void solve() {
    int mask = 1 << M;
    int ret = INF;
    for (int i = 0; i < mask; ++i) {
        char hav[15] = {0};
        for (int j = 0; j < M; ++j) {
            if (i & (1 << j)) hav[j] = 1;
        }
        ret = min(ret, gao(hav));
    }
    if (ret == INF) puts("-1");
    else printf("%d\n", ret);
}

int main() {
    while (scanf("%d", &N), N) {
        scanf("%d", &M);
        memset(mp, 0, sizeof (mp));
        for (int i = 0; i < M; ++i) {
            scanf("%d %d", &x[i], &y[i]);    
            mp[x[i]][y[i]] = 1;
        }
        for (int i = 0; i < M; ++i) {
            scanf("%d", &r[i]);
        }
        solve();
    }
    return 0;    
} 

 

posted @ 2013-05-29 21:29  沐阳  阅读(234)  评论(0编辑  收藏  举报