当前位置:网站首页>State compression DP
State compression DP
2022-07-29 08:41:00 【蒟】
State compression dp Usually an integer represents a state , The so-called state varies in different problems .
Patients with a :
291. Mondrian's dream
Portal
dp Stateful means , State calculation . State means , Represents a collection , It has its own attributes .
From this point of view .
Its status is expressed as dp[i][j], Its collective meaning is : When the first i-1 After the column has been filled , From i-1 Column out to i The state of the column , And this state is expressed as j.(j How to understand it as a state , take j Convert to binary representation , Each represents a line , And when the first i When one is the first i-1 There is a wooden block protruding to the i That's ok .
Code not optimized , Optimized code .
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 12, M = 1 << N;
ll dp[N][M];
bool book[M];
int main() {
int n, m;
while (cin >> n >> m, n || m) {
for (int i = 0; i < 1 << n; i++) {
book[i] = 1;
int cnt = 0;
for (int j = 0; j < n; j++) {
if (i >> j & 1) {
if (cnt & 1)
book[i] = 0;
cnt = 0;
} else cnt++;
}
if (cnt & 1)book[i] = 0;
}
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 1 << n; j++) {
for (int k = 0; k < 1 << n; k++) {
if ((j & k) == 0 && book[j | k]) {
dp[i][j] += dp[i - 1][k];
}
}
}
}
cout << dp[m][0] << endl;
}
return 0;
}
After deleting invalid combinations :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 12, M = 1 << N;
int n, m;
ll dp[N][M];
bool book[M];
vector<int>status[M];
int main() {
while (cin >> n >> m, n || m) {
for (int i = 0; i < 1 << n; i++) {
bool is_valid = true;
int cnt = 0;
for (int j = 0; j < n; j++) {
if (i >> j & 1) {
if (cnt & 1) {
is_valid = false;
break;
}
cnt = 0;
} else cnt++;
}
if (cnt & 1)is_valid = false;
book[i] = is_valid;
}
for (int i = 0; i < 1 << n; i++) {
status[i].clear();
for (int j = 0; j < 1 << n; j++) {
if ((i & j) == 0 && book[i | j]) {
status[i].push_back(j);
}
}
}
memset(dp,0,sizeof (dp));
dp[0][0]=1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 1 << n; j++) {
for (auto it : status[j]) {
dp[i][j] += dp[i - 1][it];
}
}
}
cout << dp[m][0] << endl;
}
return 0;
}
Example 2 : The shortest Hamilton route
#include<bits/stdc++.h>
using namespace std;
const int N = 21, M = 1 << N;
int dp[M][N];
int mp[N][N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &mp[i][j]);
}
}
memset(dp,0x3f,sizeof (dp));
dp[1][0] = 0;
for (int i = 0; i < 1 << n; i++) {
for (int j = 0; j < n; j++) {
if (i >> j & 1) {
for (int k = 0; k < n; k++) {
if ((i - (1 << j)) >> k & 1) {
dp[i][j] = min(dp[i][j], dp[i - (1 << j)][k] + mp[k][j]);
}
}
}
}
}
cout<<dp[(1<<n)-1][n-1];
return 0;
}
边栏推荐
- AI application lesson 1: C language Alipay face brushing login
- Sword finger offer 32 - ii Print binary tree II from top to bottom
- ICMP message analysis
- Common query optimization technology of data Lake - "deepnova developer community"
- Virtual augmentation and reality Part 2 (I'm a Firebird)
- 2022 electrician (elementary) test question simulation test platform operation
- Basic crawler actual combat case: obtaining game product data
- commonjs导入导出与ES6 Modules导入导出简单介绍及使用
- Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
- Week 1 task deep learning and pytorch Foundation
猜你喜欢

用户身份标识与账号体系实践

Basic shell operations (Part 1)

HC-SR04超声波测距模块使用方法和例程(STM32)

2022年R2移动式压力容器充装考题模拟考试平台操作

centos7/8命令行安装Oracle11g

2022 Shandong Province safety officer C certificate work certificate question bank and answers

Google browser cross domain configuration free

Selenium actual combat case crawling JS encrypted data

数学建模——聚类

Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
随机推荐
Centos7/8 command line installation Oracle11g
centos7/8命令行安装Oracle11g
Second week of postgraduate freshman training: convolutional neural network foundation
Collation of ml.net related resources
MFC integration QT verification and problem handling
不同数据库相同字段查不重复数据
HC-SR04超声波测距模块使用方法和例程(STM32)
2022 P cylinder filling test simulation 100 questions simulation test platform operation
Sword finger offer 50. the first character that appears only once
搜索与回溯经典题型(八皇后)
预训练模型与传统方法在排序上有啥不同?
Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
Clickhouse learning (II) Clickhouse stand-alone installation
Squareline partners with visual GUI development of oneos graphical components
Application of matrix transpose
GBase 8s数据库有哪些备份恢复方式
What are the backup and recovery methods of gbase 8s database
node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
数学建模——聚类
PostgreSQL手动创建HikariDataSource解决报错Cannot commit when autoCommit is enabled