状态压缩DP AcWing 291. 蒙德里安的梦想

状态压缩DP AcWing 291. 蒙德里安的梦想

原题链接

AcWing 291. 蒙德里安的梦想

算法标签

动态规划 状态压缩DP

思路

具体思路

状态压缩DP AcWing 291. 蒙德里安的梦想_第1张图片
状态压缩DP AcWing 291. 蒙德里安的梦想_第2张图片

代码

#include
#define int long long
#define rep(i, a, b) for(int i=a;ib;--i)
using namespace std;
const int N = 12, M = 1 << N;
int f[N][M], st[M];
vector sta[M];
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
void put(int x) {
    if(x<0) putchar('-'),x=-x;
    if(x>=10) put(x/10);
    putchar(x%10^48);
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    while(n=read(), m=read(), n||m){
    	//预处理:判断合并列的状态是否合法
		//如果合并列的某行是1表示横放,是0表示竖放
		//如果合并列不存在连续的奇数个0,即为台法状态
        rep(i, 0, 1<>j&1){// 如果是1
                    if(cnt&1){// 如果连续0的个数奇数
                        st[i]=false;// 记录i不合法
                        cnt=0;
                    }
                }
                else{// 如果是1, 记录0的个数
                    cnt++;
                }
            }
            // 处理高位0的个数 以4为例 0100 也是非法数字
            if(cnt&1){
                st[i]=false;
            }
        }
        //状态计算
        memset(f, 0, sizeof f);
        // 第0列不横放是一种合法的方案
        f[0][0]=1;
        rep(i, 1, m+1){//阶段: 枚举列
            rep(j, 0, 1<

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

你可能感兴趣的:(算法,算法)