【状压DP】chess 题解

本文介绍了一个关于在01矩阵中放置旗子的问题,通过状压DP的方法求解不同放置方案的数量。矩阵大小限制为n*m,n,m<12。详细介绍了算法实现过程,包括状态转移方程及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给出一个n*m的01矩阵,里面为0的点不能放旗子,两个相邻的格子不能放旗子,求放法种数。
n,m<12

裸的状压DP,15行,每行一个12位01串表示。check就与一下。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2147483647
#define LL long long
#define clr(x) memset(x, 0, sizeof x)
#define digit (ch <  '0' || ch >  '9')

using namespace std;

template <class T> inline void read(T &x) {
    int flag = 1; x = 0;
    register char ch = getchar();
    while( digit) { if(ch == '-')  flag = -1; ch = getchar(); }
    while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
    x *= flag;
}

const int maxn = 13;
const int MOD = 100000000;
int n,m,maxs,opt,ans,dp[maxn][1<<maxn];
int a[maxn];

inline bool check1(int s, int ss) { return (!(s&(s<<1)) && !(ss&s)); }

inline bool check2(int s, int ss) { return !(s&ss); }

int main() {
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    read(n); read(m); maxs = (1<<m)-1;
    for(register int i = 1; i <= n; i++) 
        for(register int j = 1; j <= m; j++) read(opt), a[i] += (!opt)<<(m-j);
    for(register int i = 0; i <= maxs; i++) if(check1(i, a[1])) dp[1][i] = 1;
    for(register int i = 2; i <= n; i++)
        for(register int j = 0; j <= maxs; j++) if(check1(j, a[i-1]))
            for(register int k = 0 ; k <= maxs; k++)
                if(check1(k, a[i]) && check2(j, k)) dp[i][k] = (dp[i][k]+dp[i-1][j])%MOD;
    for(register int i = 0; i <= maxs; i++) ans = (ans+dp[n][i])%MOD;
    cout << ans;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值