跳到正文

个人记录

状态压缩DP2

Hhy140516约 16 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 16 分钟

版本

  1. v1当前

基础

第二种状态压缩类题目:二维状态问题。

三种不能的状态的判定方法:

  1. 不能种:现在状态∣土地状态≠现在状态现在状态 | 土地状态\ne现在状态。
  2. 横向不能:现在状态&现在状态<<位数≠0现在状态 \& 现在状态 << 位数\ne0。
  3. 竖向不能:现在状态&上方状态<<位数≠0现在状态 \& 上方状态 << 位数\ne0。

dpi,jdp _ {i , j} 通常指前 ii 行,第 ii 行状态是 jj 的方案数。

例题

P1879 [USACO06NOV] Corn Fields G

直接用基础中的模版,将输入变成二进制,就可以了。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#define debug(...){\
	cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
	string s=#__VA_ARGS__,s2="";\
	vector<string>v;\
	for(auto i:s){\
		if(i==','){\
			v.push_back(s2);\
			s2="";\
		}else{\
			s2+=i;\
		}\
	}\
	v.push_back(s2);\
	vector<int>v2={__VA_ARGS__};\
	for(int i=0;i<v.size()-1;i++){\
		cout<<v[i]<<"="<<v2[i]<<"\n";\
	}\
	cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
using namespace std ;
const int kMaxN = 13 , MOD = 1e8 , INF = 1e18 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
struct node
{
  int x , y , id ;
}Node[kMaxN] ;
int sol[kMaxN] , n , m ;
int dp[1 << kMaxN][kMaxN] ;
inline ll ksm(ll a , ll b)
{
	ll mul = 1 ;
	while(b)
	{
		if(b & 1) mul *= a , mul %= MOD ;
		a *= a ;
		a %= MOD ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void Read()
{
	cin >> n >> m ;
	for( int i = 1 ; i <= n ; i++ )
	{
		for( int j = 1 ; j <= m ; j++ )
		{
			int a ;
			cin >> a ;
			sol[i] = sol[i] * 2 + a ;
		}
	}
}
void work()
{
  Read() ;
  dp[0][0] = 1 ;
  for( int i = 1 ; i <= n ; i++ )
  {
  	for( int j = 0 ; j < (1 << m) ; j++ )
  	{
  		if((j & (j << 1)) != 0) continue ;
			if((j & sol[i]) != j) continue ;
			for( int k = 0 ; k < (1 << m) ; k++ )
			{
				if((j & k) == 0)
				{
					dp[j][i] += dp[k][i - 1] ;
					dp[j][i] %= MOD ;
				}
			}
		}
	}
	int ans = 0 ;
	for( int i = 0 ; i < (1 << m) ; i++ )
	{
		ans += dp[i][n] ;
		ans %= MOD ;
	}
	cout << ans ;
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}

P8756 [蓝桥杯 2021 省 AB2] 国际象棋

需要加入一位在 dpdp 中,表示现有马数,注意取模。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#define debug(...){\
	cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
	string s=#__VA_ARGS__,s2="";\
	vector<string>v;\
	for(auto i:s){\
		if(i==','){\
			v.push_back(s2);\
			s2="";\
		}else{\
			s2+=i;\
		}\
	}\
	v.push_back(s2);\
	vector<int>v2={__VA_ARGS__};\
	for(int i=0;i<v.size()-1;i++){\
		cout<<v[i]<<"="<<v2[i]<<"\n";\
	}\
	cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
using namespace std ;
const int kMaxN = 13 , MOD = 1e9 + 7 , INF = 1e18 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
struct node
{
  int x , y , id ;
}Node[kMaxN] ;
int n , m , p , cal[1 << 6] ;
int dp[101][(1 << 6)][(1 << 6)][21] ;
inline ll ksm(ll a , ll b)
{
	ll mul = 1 ;
	while(b)
	{
		if(b & 1) mul *= a , mul %= MOD ;
		a *= a ;
		a %= MOD ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void Read()
{
	cin >> n >> m >> p ;
}
void work()
{
  Read() ;
  for( int i = 0 ; i < (1 << n) ; i++ )
  {
  	int j = i , cnt = 0 ;
  	while(j)
  	{
  		cnt += j % 2 ;
  		j /= 2 ;
		}
		cal[i] = cnt ;
	}
  dp[0][0][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++ )
			{
				for( int l = 0 ; l < (1 << n) ; l++ )
				{
					for( int h = cal[j] ; h <= p ; h++ )
					{
						if(((j & (k << 1)) || (j & (k >> 1)) || ((j & (l << 2)) || (j & (l >> 2)))) == 0)
						{
							dp[i][l][j][h] += dp[i - 1][k][l][h - cal[j]] ;
							dp[i][l][j][h] %= MOD ;
						}
					}
				}
			}
		}
	}
	int ans = 0 ;
	for( int i = 0 ; i < (1 << n) ; i++ )
	{
		for( int j = 0 ; j < (1 << n) ; j++ )
		{
			ans += dp[m][i][j][p] ;
			ans %= MOD ;
		}
	}
	cout << ans ;
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}

P2704 [NOI2001] 炮兵阵地

上两题的结合体,只是要求最大值。

C++
//Code by hhy
#include<bits/stdc++.h>
#define F(i , a , b , c) for( int i = (a) ; ((c > 0) ? i <= (b) : i >= (b)) ; i += c )
#define T(i , root , b , c) for( int i = root ; b ; c )
#define int long long
#define W(f) while(f)
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define ll long long
#define debug(...){\
	cout<<"debug in function "<<__FUNCTION__<<",line "<<__LINE__<<"\n";\
	string s=#__VA_ARGS__,s2="";\
	vector<string>v;\
	for(auto i:s){\
		if(i==','){\
			v.push_back(s2);\
			s2="";\
		}else{\
			s2+=i;\
		}\
	}\
	v.push_back(s2);\
	vector<int>v2={__VA_ARGS__};\
	for(int i=0;i<v.size()-1;i++){\
		cout<<v[i]<<"="<<v2[i]<<"\n";\
	}\
	cout<<v[v.size()-1]<<"="<<v2[v2.size()-1]<<"\n\n";\
}
using namespace std ;
const int kMaxN = 13 , MOD = 1e9 + 7 , INF = 1e18 ;
struct Edgepr
{
	int u , w ;
};
struct Edgeve
{
	int v , w ;
};
struct node
{
  int x , y , id ;
}Node[kMaxN] ;
int n , m , op[1 << 10] , cal[1 << 10] , sol[1005] , cnt ;
int dp[105][105][105] ;
inline ll ksm(ll a , ll b)
{
	ll mul = 1 ;
	while(b)
	{
		if(b & 1) mul *= a , mul %= MOD ;
		a *= a ;
		a %= MOD ;
		b >>= 1 ;
	}
	return mul ;
}
inline int read()
{
    int x = 0 , f = 1 ;
    char ch = getchar() ;
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1 ;
        ch = getchar() ;
    }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0' , ch = getchar() ;
    return x * f ;
}
void write(int x)
{
    if(x < 0) putchar('-') , x = -x ;
    if(x > 9) write(x / 10) ;
    putchar(x % 10 + '0') ;
}
void Read()
{
	cin >> n >> m ;
	for( int i = 1 ; i <= n ; i++ )
	{
		for( int j = 1 ; j <= m ; j++ )
		{
			char c ;
			cin >> c ;
			if(c == 'P') sol[i] += (1 << (j - 1)) ;
		}
	}
}
void work()
{
  Read() ;
  for( int i = 0 ; i < (1 << m) ; i++ )
  {
  	if(((i & (i << 1)) || (i & (i << 2))) == 0)
  	{
  		op[cnt] = i ;
  		int j = i , cn = 0 ;
	  	while(j)
	  	{
	  		cn += j % 2 ;
	  		j /= 2 ;
			}
			cal[cnt] = cn ;
			cnt++ ;
		}
	}
//	cout << cnt << endl;
	memset(dp , -1 , sizeof dp) ;
  dp[0][0][0] = 0 ;
  int maxn = 0 ;
  for( int i = 1 ; i <= n ; i++ )
  {
  	for( int j = 0 ; j < cnt ; j++ )
  	{
  		if((op[j] & sol[i]) != op[j]) continue ;
			for( int k = 0 ; k < cnt ; k++ )
			{
				if((op[j] & op[k]) != 0) continue ;
				for( int l = 0 ; l < cnt ; l++ )
				{
					if((op[l] & op[j]) != 0) continue ;
					if(dp[i - 1][k][l] == -1) continue ;
					dp[i][j][k] = max(dp[i][j][k] , dp[i - 1][k][l] + cal[j]) ;
//					cout << dp[i][j][k] << endl;
					maxn = max(maxn , dp[i][j][k]) ;
				}
			}
		}
	}
	cout << maxn ;
}
signed main()
{
//	freopen(".in" , "r" , stdin) ;
//	freopen(".out" , "w" , stdout) ;
	ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int t = 1 ;
  // cin >> t ;
  while(t--) work() ;
  return 0 ;
}
原文