• 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;
    }
  • 相关阅读:
    IDEA 修改JavaWeb的访问路径
    坦克大战--Java类型 ---- (1)音乐播放
    codeforces 620C
    算法笔记之KMP算法
    算法笔记数组
    26. Remove Duplicates from Sorted Array
    哈夫曼树的证明
    Complete Binary Search Tree
    Root of AVL Tree
    .net framework环境
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9251104.html
Copyright © 2020-2023  润新知