hdu 4152暴搜

简单题,暴搜就可以过了。

/*
 * hdu4152/win.cpp
 * Created on: 2012-7-28
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXN = 22;
const int MAXM = 18;
typedef struct {
    int num;
    int lists[MAXM];
    void init() {
        num = 0;
    }
}Record;
inline bool operator<(const Record &r1, const Record &r2) {
    if(r1.num != r2.num) {
        return r1.num > r2.num;
    }else {
        for(int i = 0; i < r1.num; i++) {
            if(r1.lists[i] != r2.lists[i]) {
                return r1.lists[i] < r2.lists[i];
            }
        }
    }
    return true;
}
int N, M, goals[MAXN], data[MAXM][MAXN];
bool use[MAXM];
Record ans;

bool judge() {
    int tgoals[MAXN];
    memcpy(tgoals, goals, sizeof(goals));
    for(int i = 0; i < M; i++) {
        if(!use[i]) {
            continue;
        }
        for(int j = 0; j < N; j++) {
            tgoals[j] -= data[i][j];
        }
    }
    int j;
    for(j = 0; j < N; j++) {
        if(tgoals[j] > 0) {
            break;
        }
    }
    return j >= N;
}

void dfs(int m) {
    if(m > 0) {
        use[m - 1] = true;
        dfs(m - 1);
        use[m - 1] = false;
        dfs(m - 1);
        return;
    }
    if(!judge()) {
        return ;
    }
    Record temp;
    temp.init();
    for(int i = 0; i < M; i++) {
        if(use[i]) {
            temp.lists[temp.num++] = i + 1;
        }
    }
    if(temp < ans) {
        ans = temp;
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    while(scanf("%d", &N) == 1) {
        for(int i = 0; i < N; i++) {
            scanf("%d", &goals[i]);
        }
        scanf("%d", &M);
        for(int i = 0; i < M; i++) {
            for(int j = 0; j < N; j++) {
                scanf("%d", &data[i][j]);
            }
        }
        memset(use, false, sizeof(use));
        ans.init();
        dfs(M);
        printf("%d", ans.num);
        for(int i = 0; i < ans.num; i++) {
            printf(" %d", ans.lists[i]);
        }
        putchar('\n');
    }
    return 0;
}
posted @ 2012-07-28 12:48  moonbay  阅读(277)  评论(0编辑  收藏  举报