UVa 11795 - Mega Man's Mission (集合DP 状态压缩)

Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

Description

Download as PDF

 

B

Mega Man’s Missions

Input

Standard Input

Output

Standard Output

 

Mega Man is off to save the world again. His objective is to kill the Robots created by Dr. Wily whose motive is to conquer the world. In each mission, he will try to destroy a particular Robot. Initially, Mega Man is equipped with a weapon, called the Mega Buster which can be used to destroy the Robots.  Unfortunately, it may happen that his weapon is not capable of taking down every Robot. However, to his fortune, he is capable of using the weapons from Robots which he has completely destroyed and these weapons maybe able to take down Robots which he otherwise cannot with his own weapon. Note that, each of these enemy Robots carry exactly  one weapon themselves for fighting Mega Man.  He is able to take down the Robots in any order as long as he has at least one weapon capable of destroying the Robot at a particular mission. In this problem, given the information about the Robots and their weapons, you will have to determine the number of ways Mega Man can complete his objective of destroying all the Robots.

 

Input

Input starts with an integer T(T≤50), the number of test cases.

Each test case starts with an integer N(1≤N≤16). Here N denotes the number of Robots to be destroyed (each Robot is numbered from 1 to N). This line is followed by N+1 lines, each containing N characters. Each character will either be ‘1’ or ‘0’. These lines represent a (N+1)*N matrix. The rows are numbered from 0 to N while the columns are numbered from 1 to N. Row 0 represents the information about the “Mega Buster”. The jth character of Row 0 will be ‘1’if the “Mega Buster” can destroy the jth Robot. For the remaining N rows, the jth character of ith row will be ‘1’ if the weapon of ith Robot can destroy the jth Robot. Note that, a Robot’s weapon could be used to destroy the Robot itself, but this will have no impact as the Robot must be destroyed anyway for its weapon to be acquired.

Output

For each case of input, there will be one line of output. It will first contain the case number followed by the number of ways Mega Man can complete his objective. Look at the sample output for exact format.

 

Sample Input

Sample Output

3

1

1

1

2

11

01

10

3

110

011

100

000

 

Case 1: 1

Case 2: 2

Case 3: 3

 

 


题意:

最初只有一个武器,n个机器人,每个机器人有一个武器,每个武器能消灭特定的一些机器人,消灭一个机器人就能得到他的武器。问消灭所有机器人的顺序总数。


dp[s] 表示消灭s这个集合中机器人的方法数。

dp[s] 这个状态可以由其它状态通过消灭机器人i得到。也就是

dp[s] = sum { dp[s^(1<<i)] }; i为所有s^(1<<i)(表示还没有杀死i)这个集合能够杀死并且在s这个集合里面所有机器人; 


dp[s] 中的s表示的是几经杀死s这个集合中的机器人方法数

canKill[s] 表示杀死s这个集合中的机器人后能够杀死的机器人集合



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 16 + 4;
const int maxs = 1<<17;
int A[maxn];
int canKill[maxs];
LL dp[maxs];
int n;
LL ans;

char s[20];

int main() {
    int T;

    scanf("%d", &T);
    for(int kase=1; kase<=T; kase++) {
        scanf("%d", &n);
        int S0 = 0;
        scanf("%s", s);
        for(int i=0; i<n; i++) {
            if(s[i]-'0') S0 |= 1<<i;
        }
        for(int i=0; i<n; i++) {
            A[i] = 0;
            scanf("%s", s);
            for(int j=0; j<n; j++) {
                if(s[j]-'0') A[i] |= 1<<j;
            }
        }
        int maxState = (1<<n) - 1;
        for(int s=0; s<=maxState; s++) {
            canKill[s] = S0;
            for(int i=0; i<n; i++) if(s&(1<<i))
                canKill[s] |= A[i];
        }
        dp[0] = 1;
        for(int s=1; s<=maxState; s++) {
            dp[s] = 0;
            for(int i=0; i<n; i++) if((s&(1<<i)) && (canKill[s^(1<<i)]&(1<<i))) {
                dp[s] += dp[s^(1<<i)];
            }
        }
        printf("Case %d: ", kase);
        P64I(dp[maxState]);
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值