uva_11795_Mega Man's Mission( 簡單狀壓DP )

題意:

分析:

略,注意精度

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

#define REPI(i, s, e)   for(int i = (s); i <= (e); i ++)
#define REPD(i, e, s)   for(int i = (e); i >= (s); i --)

const int N    = 16;
const int MAXN = 65536;

char str[N+1];
int n, g[N+1][N+1], vis[MAXN+1];
long long f[MAXN+1];

int input(void)
{
        scanf("%d", &n);
        REPI(i, 0, n) {
                scanf("%s", str);
                REPI(j, 1, n) {
                        g[i][j] = str[j-1]-'0';
                }
        }
}

inline int valid(int s, int idx)
{
        if( g[0][idx] ) {
                return 1;
        }
        REPI(i, 0, n-1) {
                if( s&(1<<i) && g[i+1][idx] ) {
                        return 1;
                }
        }
        return 0;
}

inline long long dp(int s)
{
        if( -1LL != f[s] ) {
                return f[s];
        }

        f[s] = 0LL;
        REPI(i, 0, n-1) {
                if( s&(1<<i) && valid(s^(1<<i), i+1) ) {
                        f[s] += dp(s^(1<<i));
                }
        }
        return f[s];
}

long long process(void)
{
        int final = (1<<n)-1;
        REPI(i, 0, final+1) {
                f[i] = -1LL;
        }
        f[0] = 1LL;
        return dp(final);
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int cas, cnt(1);
        scanf("%d", &cas);
        REPI(k, 1, cas) {
                input();
                printf("Case %d: %lld\n", cnt ++, process());
        }
        return 0;
}

Code:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值