当前位置:网站首页>状态压缩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;
}边栏推荐
- Anti shake and throttling
- "Food alliance ordering system"
- Some operations of Ubuntu remote server configuration database (unable to locate package MySQL server, steps of installing mysql, unable to enter password when logging in MySQL)
- 🧐 table1 | 一秒搞定你的三线表
- Tips for API interface optimization
- SurfaceControl和SurfaceFlinger通信
- Introduction of shortest path tree (SPT) and matlab code
- DRF - paging, JWT introduction and principle, JWT quick use, JWT source code analysis, JWT custom return format, custom user issued token, custom token authentication class
- Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
- Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
猜你喜欢

AQS原理

将行内元素转换为块元素的方法

IMG tags prohibit dragging pictures

Jupyter notebook中5个有趣的魔法命令
![[development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine](/img/a1/9a69e5d123a8a11504da251bd1bcfc.png)
[development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine

Tips for API interface optimization

自制 | 纯手工自制一个16位RISC架构CPU

iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器

Nftscan and nftplay have reached strategic cooperation in the field of NFT data

芯驰科技发布G9系列最新旗舰产品,配备6个1.8Ghz主频的A55核心
随机推荐
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
Relying on cloud business to support revenue growth alone, is Microsoft still overvalued?
Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
Matlab02: structured programming and function definition "suggestions collection"
【Web开发】Flask框架基础知识
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
The 30th day of question brushing
selenium对接代理与seleniumwire访问开发者工具NetWork
[untitled]
Upload Excel files with El upload and download the returned files
rk3399 9.0驱动添加Powser按键
多线程顺序运行的几种方法,面试可以随便问
andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
SDRAM控制器设计(数字控制器的两种设计方法)
追踪伦敦银实时行情的方法有哪些?
Brief introduction to compressed sensing
CDN mode uses vant components, and components cannot be called after they are introduced
【目标检测】YOLOR理论简介+实践测试VisDrone数据集
ZABBIX deployment and monitoring