当前位置:网站首页>状态压缩dp-蒙德里安的梦想
状态压缩dp-蒙德里安的梦想
2022-07-28 23:39:00 【znyee07】
题目链接:
291. 蒙德里安的梦想 - AcWing题库https://www.acwing.com/problem/content/description/293/
f[i][j]表示:前 i - 1 列已经放好,并且从第i - 1 列向第 i 列延申的状态是 j
核心:
摆放的小方格方案数 等价于 横着摆放的小方格方案数
如何判断当前方案是否合法?
遍历每一列,i列的方案数只和i-1列有关系
1. j&k==0, i-2列伸到i-1的小方格 和i-1列放置的小方格 不重复。
2. 每一列,所有连续着空着的小方格必须是偶数个
#include<bits/stdc++.h>
using namespace std;
const int N = 10, M = 1 << N;
//M 二进制的每一位存储一位状态
int st[M];
//储存每一列上合法的摆放状态
long long f[N][M];
//f[i][j]表示:前 i-1 列已经放好,并且从第i-1 列向第 i列延申的状态是j
// & 只有两位都为 1 才为 1
// | 有一个 1 就是 1
// ^ 两数不同为 1
int main()
{
int n, m; cin >> n >> m;
for(int i = 0; i < 1 << n; i++)//枚举每一列的占位状态中那些是合法的
{ //i * 2的 n次方,i的 2进制数左移 n位,
//一共 n 列,每一个格子有两种情况,为 0(不放),为 1(放)
int cnt = 0;
// 记录一列中 0 的个数
st[i] = true;
for(int j = 0; j < n; j++)//从低到高枚举每一位
if(i >> j & 1)//如果该位为 1
{
if(cnt & 1)//cnt 为奇数时为真
//cnt & 1 为真,表示之前连续的 0 为奇数
st[i] = false;
cnt = 0;
}
else cnt ++;
// 该位为 0, 0 的个数++
if(cnt & 1) st[i] = false;
//到末尾了 判断一下前面 0 的个数
}
f[0][0] = 1;
//不存在 -1列,所以第 0行第 0列 不会有延伸出来的横放小方块
for(int i = 1; i <= m; i++)// 遍历每一列
{
for(int j = 0; j < 1 << n; j++)//遍历 i列每一种状态
{
for(int k = 0; k < 1 << n; k++)//遍历 i - 1列每一种状态
if((j & k) == 0 && (st[j | k]))
// 两个转移条件:
//i 列和 i - 1列同一行不同时捅出来 ;
//本列捅出来的状态 j 和上列捅出来的状态 k 求或,
//得到上列是否为奇数空行状态,奇数空行不转移。
//st[j | k] == 1 表示 在 i 列状态 j, i - 1 列状态 k 的情况下是合法的.
f[i][j] += f[i - 1][k];
//状态转移
}
}
cout<< f[m][0];
//前 m - 1列已经放置好, 且没有m - 1列向 m 列延伸出
return 0;
}
边栏推荐
- flask结合容联云发送验证码
- Asynchronous mode worker thread
- Send SMS verification code asynchronously using Ronglian cloud celery
- [network security] complete the blacklist and whitelist functions of server firewall through iptables and ipset
- Data warehouse construction - ads floor
- Common sparse basis and matlab code for compressed sensing
- Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology
- Have you seen the management area decoupling architecture? Can help customers solve big problems
- Outlier detection and open set identification (2)
- seleniumwire获取百度指数
猜你喜欢
[basic course of flight control development 8] crazy shell · open source formation uav-i2c (laser ranging)
IMG tags prohibit dragging pictures
JWT token related configuration (global configuration identity authentication rewrites authenticate method)
将行内元素转换为块元素的方法
Outlier detection and Gan network (1)
Method of converting inline elements to block elements
Shell编程规范与变量
Data warehouse construction - ads floor
Upload Excel files with El upload and download the returned files
Meeting notification & meeting feedback & feedback details function of meeting OA project
随机推荐
Protective copy & stateless
【MySQL 8】Generated Invisible Primary Keys(GIPK)
[basic course of flight control development 8] crazy shell · open source formation uav-i2c (laser ranging)
管理区解耦架构见过吗?能帮客户搞定大难题的
Requestvideoframecallback() simple instance
浅谈一下跨端技术方案
Techo Hub 福州站干货来袭|与开发者共话工业智能新技术
B+ 树 ~
Outlier detection and open set identification (2)
andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
数学建模及其基础知识详解(化学常考知识点)
返回*this的成员函数
【无标题】
Shell programming specifications and variables
时间序列数据的预处理方法总结
SurfaceControl和SurfaceFlinger通信
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
数仓搭建——ADS层
NFT 项目的 7 种市场营销策略
【目标检测】YOLOR理论简介+实践测试VisDrone数据集