洛谷 P1879 玉米田(状压DP入门题)

传送门

https://www.cnblogs.com/violet-acmer/p/9852294.html

题解:

  相关变量解释:

 int M,N;
int plant[maxn][maxn];//草场情况
struct Node
{
int status;//状态
int res;//方案
Node(int a=,int b=):status(a),res(b){}
};
vector<Node >dp[maxn];//dp[i][j] : 第i行的j状态能达到的最大方案

  根据dp定义,很容易写出状态转移方程:

 for(int i=;i <= M;++i)
{
for(int j=;j <= maxNum;++j)
{
int res=Find(j,i-);//查找与上一次决策没有相邻的草地的决策个数
//isSat1() : 判断草地是否合法,即判断不含有相邻草场
//isSat2() : 判断当前决策是否有相邻的草地
if(isSat1(i,j) && isSat2(j) && res)
dp[i].pb(Node(j,res));
}
}

AC代码:

 #include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define R(x) (1<<x)
#define pb(x) push_back(x)
const int MOD=1e8;
const int maxn=+; int M,N;
int plant[maxn][maxn];//草场情况
struct Node
{
int status;//状态
int res;//方案
Node(int a=,int b=):status(a),res(b){}
};
vector<Node >dp[maxn];//dp[i][j] : 第i行的j状态能达到的最大方案
bool isSat1(int i,int x)//判断草地是否合法
{
int index=N;
for(int j=;j <= N;++j)
if(plant[i][index--] == && (R(j-)&x) != )
return false;
return true;
}
bool isSat2(int x)//判断当前决策是否有相邻的草地
{
for(int j=;;++j)
{
int val=R(j-)+R(j-);
if(val > x)
return true ;
if((val&x) == val)
return false;
}
return true;
}
int Find(int now,int i)//查找与上一次决策没有相邻的草地的决策个数
{
int res=;
for(int j=;j < dp[i].size();++j)
{
Node node=dp[i][j];
int pre=node.status;
if((pre&now) == )
res=res%MOD+node.res;
}
return res%MOD;
}
void Solve()
{
int maxNum=(<<N)-;
dp[].pb(Node(,));
for(int i=;i <= M;++i)
{
for(int j=;j <= maxNum;++j)
{
int res=Find(j,i-);
if(isSat1(i,j) && isSat2(j) && res)
dp[i].pb(Node(j,res));
}
}
int res=;
for(int i=;i < dp[M].size();++i)
res=res%MOD+dp[M][i].res;
printf("%d\n",res%MOD);
}
int main()
{
scanf("%d%d",&M,&N);
for(int i=;i <= M;++i)
for(int j=;j <= N;++j)
scanf("%d",plant[i]+j);
Solve();
}
上一篇:工具:BT Sync 同步文件


下一篇:HTML5:图片、音乐和视频