• 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;
    }
  • 相关阅读:
    4种排序实践
    redis 应用场景和数据类型
    建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C
    分布式线程安全(redis、zookeeper、数据库)
    [专项]3道改错题
    kafka 业务埋点
    spring boot集成kafka
    kafka本地调试
    C语言 gets()和scanf()函数的区别
    EOF
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9251104.html
Copyright © 2020-2023  润新知