Mega Man's Mission UVA - 11795(压状DP)

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 20;
const int maxs = (1 << 16) + 10;
int wp[maxn], cankill[maxs];   // wp[i]表示击败i获得的武器攻击范围,cankill[s]表示击杀集合为s后攻击范围
ll d[maxs];    // d[s], s是二进制0表示存活,1表示被击杀,d[s]表示到达目前状态下的路条数
char s[maxn];

int main()
{
    // freopen("/Users/maoxiangsun/MyRepertory/acm/i.txt", "r", stdin);
    // freopen("/Users/maoxiangsun/MyRepertory/acm/o.txt", "w", stdout);
    int T;           
    scanf("%d", &T); 
    int cas=0;                   
    while(T--) {
        int n;
        scanf("%d", &n);
        for(int i = 0; i <= n; i++) {      
            scanf("%s", s);        
            wp[i] = 0;
            for(int j = 0; j < n; j++) {
                if(s[j]-'0') wp[i] |= (1<<j);
            }
        }
        int nState = (1 << n) - 1;
        cankill[0] = wp[0];
        for(int i = 1; i <= nState; i++) {
            cankill[i] = wp[0];
            for(int j = 0; j < n; j++) {
                if((1 << j) & i) cankill[i] |= wp[j + 1];
            }
        }
        memset(d, 0, sizeof(d));
        d[0] = 1;
        for(int s = 1; s <= nState; s++) {
            for(int j = 0; j < n; j++) {
                if(((1 << j) & s) && (cankill[s ^ (1 << j)] & (1 << j))) {
                    d[s] += d[s ^ (1 << j)];
                }
            }
        }   
        printf("Case %d: %lld\n", ++cas, d[nState]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunmaoxiang/article/details/88125244
今日推荐