当前位置:网站首页>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;
}
边栏推荐
- C language calculates the length of string
- MySQL statement mind map
- Markdown concise grammar manual
- C language -- 22 one dimensional array
- Cluster usage specification
- 状态压缩dp
- 7.1-default-arguments
- Sword finger offer 50. the first character that appears only once
- 2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
- (视频+图文)机器学习入门系列-第3章 逻辑回归
猜你喜欢

AI application lesson 1: C language Alipay face brushing login

Day6: use PHP to write file upload page

Is the sub database and sub table really suitable for your system? Talk about how to select sub databases, sub tables and newsql

Osg3.6.5 failed to compile freetype

Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"

Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings

Day15: the file contains the vulnerability range manual (self use file include range)

User identity identification and account system practice

C language watch second kill assist repeatedly

Application of matrix transpose
随机推荐
7.3-function-templates
解决Base64 报错 Illegal base64 character
What if official account does not support markdown format file preparation?
access数据库可以被远程访问吗
Data warehouse layered design and data synchronization,, 220728,,,,
leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
RPC和REST
6.3 references
Clickhouse learning (I) Clickhouse?
Personal study notes
Intel will gradually end the optane storage business and will not develop new products in the future
(视频+图文)机器学习入门系列-第2章 线性回归
Hc-sr04 use method and routine of ultrasonic ranging module (STM32)
To create a thread pool for the rate, start the core thread
数学建模——聚类
Curl -v | JQ
7.2-function-overloading
Normal visualization
2022 electrician (elementary) test question simulation test platform operation
Importerror: no module named XX