UVa11795 Mega Man's Mission

Posted wsmrxc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa11795 Mega Man's Mission相关的知识,希望对你有一定的参考价值。

出师不利, 一路写了 2H+ 才AC。。

其实挺简单一个状压dp, 结果因为脑残把状态的更新顺序写反了导致把DP写成了暴力。。。

吃一堑长一智吧。以后写DP之前先想好转移顺序。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 16 + 2;

int N;
int wea[MAXN], s;
long long f[(1 << MAXN)];
string str;

inline int lowbit(int x) {return x & (-x);}

long long dp(int state)
{
    if(f[state] != -1) return f[state];
    f[state] = 0;
    int weapon = s;
    for(int i = 0; i < N; i++)
        if(!(state & (1 << i))) weapon |= wea[i];

    for(int i = state; i; i -= lowbit(i)){
        int cur = lowbit(i);
        if(weapon & cur) f[state] += dp(state ^ cur);
    }
    return f[state];
}

int main()
{
    ios::sync_with_stdio(false);
    int T, t = 0; cin>>T;
    while(T--)
    {
        cin>>N;
        cin>>str; s = 0;
        for(int i = 0; i < (int) str.size(); i++) 
            s |= ((str[i] == 1) << i);
        
        memset(wea, 0, sizeof(wea));
        for(int i = 0; i < N; i++)
        {
            cin>>str;
            for(int j = 0; j < (int) str.size(); j++)
                wea[i] |= ((str[j] == 1) << j);
        }
        memset(f, -1, sizeof(f));
        f[0] = 1LL;
        cout<<"Case "<<++t<<": "<<dp((1 << N) - 1)<<endl;
    }
    return 0;
}

 

以上是关于UVa11795 Mega Man's Mission的主要内容,如果未能解决你的问题,请参考以下文章

UVa11795 Mega Man's Mission

UVA11795 Mega Man's Mission

c_cpp UVa 11795 - Mega Man的使命

UVa 658 - It's not a Bug, it's a Feature!(Dijkstra + 隐式图搜索)

How Many O's? UVA - 11038

uva 658It's not a Bug, it's a Feature!(图论--Dijkstra算法+二进制表示)