UVa 11795 Mega Man's Mission(动态规划-状态压缩DP)


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

 

 

题目大意:

T组测试数据, 每组测试数据1个n,表示要杀n个人,接下来1行表示一开始,拥有杀哪些人的武器,例如“110”表示有杀第1,2个人的武器,接下来n行,分别表示杀完第i(1<=i<=n)个人可以得到的武器。


解题思路:

状态压缩DP,转移方程就是 dp[x] = sum(dp[y]),其中y满足两个条件,(1)y是x的子状态,也就是y再杀一个人k就到x。(2)y拥有杀k的武器。


参考代码:

#include <iostream>
using namespace std;

const int MAXN = 1 << 16;
int n, weapon[MAXN], temp[MAXN], nCase, cnt;
long long x, ans, dp[MAXN];

int ch2to10(long long x) {
    int t = 1, ans = 0;
    while (x) {
        ans += (x % 10) * t;
        x /= 10;
        t *= 2;
    }
    return ans;
}

void input() {
    cin >> n;
    cin >> x;
    weapon[0] = ch2to10(x);
    for (int i = 1; i <= n; i++) {
        cin >> x;
        temp[i] = ch2to10(x);
    }
}

long long DP(int x) {
    if (dp[x] != -1) return dp[x];
    dp[x] = 0;
    for (int i = 0; i < n; i++) {
        if (x & (1 << i)) {
            if (weapon[x - (1 << i)] & (1 << i)) {
                dp[x] += DP(x - (1 << i));
            }
        }
    }
    return dp[x];
}

void work() {
    dp[0] = 1;
    for (int i = 1; i < (1 << n); i++) {
        weapon[i] = weapon[0];
        dp[i] = -1;
        for (int j = n-1; j >= 0; j--) {
            if (i & (1 << j)) {
                weapon[i] |= temp[n-j];
            }
        }
    }
    ans = DP((1 << n) - 1);
}

void output() {
    cout << "Case " << ++cnt << ": " << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> nCase;
    while (nCase--) {
        input();
        work();
        output();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值