UVA - 11795 Mega Man's Mission

题意:求消灭掉所有机器人的方法有多少,每消灭完一个就可以拿起它的武器,DP的状态转移,对于一个状态来说,要想干掉一个i,一定是从不包含i的状态转移过来的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 10010;
int n,maxState;
int p[20];
char str[20];
int kill[(1<<16)+10];
long long f[(1<<16)+10];

int main(){
    int t,cas=1;
    scanf("%d",&t);
    while (t--){
        scanf("%d",&n);
        for (int i = 0; i <= n; i++){
            scanf("%s",str);
            p[i] = 0;
            for (int j = 0; str[j]; j++)
                if (str[j] == '1')
                    p[i] |= (1<<j);
        }
        maxState = (1<<n) - 1;
        kill[0] = p[0];
        for (int s = 1; s <= maxState; s++){
            kill[s] = p[0];
            for (int i = 0; i < n; i++)
                if ((s>>i) & 1)
                    kill[s] |= p[i+1];
        }
        memset(f,0,sizeof(f));
        f[0] = 1;
        for (int s = 1; s <= maxState; s++){
            f[s] = 0;
            for (int i = 0; i < n; i++)
                if ((kill[s^(1<<i)]>>i) & 1)
                    f[s] += f[s^(1<<i)];
        }
        printf("Case %d: %lld\n",cas++,f[maxState]);
    }
    return 0;
}



你可能感兴趣的:(UVA - 11795 Mega Man's Mission)