状态DP入门---poj3524
来源:互联网 发布:pc蛋蛋超级算法公式 编辑:程序博客网 时间:2024/06/06 14:25
Corn Fields
Description
Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.
Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.
Input
Line 1: Two space-separated integers: M and N
Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)
Output
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 31 1 10 1 0
Sample Output
9
Hint
Number the squares as follows:
1 2 3 4
There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.
题目大意:农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择
解题思路:
预备知识:
动态规划的状态有时候比较难,不容易表示出来,需要用一些编码技术,把状态压缩的用简单的方式表示出来。典型方式:当需要表示一个集合有哪些元素时,往往利用2进制用一个整数表示。
*:一般有个数据 n<16 或者 n<32 这个很可能就是状态DP的标志,因为我们要用一个int的二进制来表示这些状态。要注意好这些数据规模的提示作用。
*:确定了为状态DP,那么第一步就是预处理,求出每行所有可能的状态了,cnt记录总的状态数,stk[]记录所有的可能状态。
(上面的都是理论,我们上样例,题目样例)
这种用一个数来表示一组数,以降低表示状态所需的维数的解题手段,就叫做状态压缩。
这就是一行中可能的情况,接下来我们就来预处理掉一些不要的情况
预处理
先上代码在讲解:
inline bool ok(int x){ //判断状态x是否可行
if(x&x<<1) return false;//若存在相邻两个格子都为1,则该状态不可行
return true;
}
判断状态x是否可行,解释一下这函数的功能:
值不为0,则有相邻的点
void init(){ //遍历所有可能的状态
top = 0;
int total = 1 << N; //遍历状态的上界
for(int i = 0; i < total; ++i){
if(ok(i))state[++top] = i;
}
}
本题数据:
state[]={0, 0, 1, 2, 4, 5, 0 }
主代码。
以dp[i][state(j)]来表示对于前i行,第i行采用第j种状态时可以得到的可行方案总数!
dp[i][state(j)]=dp[i-1][state(k1)]+dp[i-1][state(k2)]+......+dp[i-1][state(kn)](kn即为上一行可行状态的编号,上一行共有n种可行状态)
最终ans=dp[m][state(k1)]+dp[m][state(k2)]+......+dp[m][state(kn)]; (kn即为最后一行(第m行)可行状态的编号)
#include <cstdio>
#include <cstring>
using namespace std;
#define mod 100000000
int M,N,top = 0;
//top表示每行最多的状态数
int state[600],num[110];
//state存放每行所有的可行状态(即没有相邻的状态
//
int dp[20][600];
//dp[i][j]:对于前i行数据,每行有前j种可能状态时的解
int cur[20];
//cur[i]表示的是第i行整行的情况
inline bool ok(int x){ //判断状态x是否可行
if(x&x<<1) return false;//若存在相邻两个格子都为1,则该状态不可行
return true;
}
void init(){ //遍历所有可能的状态
top = 0;
int total = 1 << N; //遍历状态的上界
for(int i = 0; i < total; ++i){
if(ok(i))state[++top] = i;
}
}
inline bool fit(int x,int k){ //判断状态x 与第k行的实际状态的逆是否有‘重合’
if(x&cur[k])return false; //若有重合,(即x不符合要求)
return true; //若没有,则可行
}
int main(){
freopen("in.txt","r",stdin);
while(scanf("%d%d",&M,&N)!= EOF){
init();
memset(dp,0,sizeof(dp));
for(int i = 1; i <= M; ++i){
cur[i] = 0;
int num;
for(int j = 1; j <= N; ++j){ //输入时就要按位来存储,cur[i]表示的是第i行整行的情况,每次改变该数字的二进制表示的一位
scanf("%d",&num); //表示第i行第j列的情况(0或1)
if(num == 0) //若该格为0
cur[i] +=(1<<(N-j)); //则将该位置为1(注意要以相反方式存储,即1表示不可放牧
/*这里需要取反,当第1行为110的时候,取反后为001,在去跟state相与,当结果 是不存在的,就像101的情况,在第1行中,不可能出现101中1的位置
*/
}
}
//初始化dp[][];符合不相邻,而且符合第一行排放的位置
for(int i = 1;i <= top;i++){
if(fit(state[i],1)){ //判断所有可能状态与第一行的实际状态的逆是否有重合
dp[1][i] = 1; //若第1行的状态与第i种可行状态吻合,则dp[1][i]记为1
}
}
/*
状态转移过程中,dp[i][k] =Sigma dp[i-1][j] (j为符合条件的所有状态)
*/
for(int i = 2; i <= M; ++i){ //i索引第2行到第M行
for(int k = 1; k <= top; ++k){ //该循环针对所有可能的状态,找出一组与第i行相符的state[k]
if(!fit(state[k],i))continue; //判断是否符合第i行实际情况,相当于相邻的情况已经排除了
for(int j = 1; j <= top ;++j){ //找到state[k]后,再找一组与第i-1行符合,且与第i行(state[])不冲突的状态state[j]
if(!fit(state[j],i-1))continue; //判断是否符合第i-1行实际情况
if(state[k]&state[j])continue; //判断是否与第i行冲突
dp[i][k] = (dp[i][k] +dp[i-1][j])%mod; //若以上皆可通过,则将'j'累加到‘k'上
}
}
}
int ans = 0;
for(int i = 1; i <= top; ++i){ //累加最后一行所有可能状态的值,即得最终结果!!!泥马写注释累死我了终于写完了!
ans = (ans + dp[M][i])%mod;
}
printf("%d\n",ans);
}
}
文章查阅多位大神文章写的一篇笔记
大神链接:http://blog.csdn.net/harrypoirot/article/details/23163485
阅读全文
0 0
- 状态DP入门---poj3524
- poj3524 Corn Fields(状态压缩dp)
- poj3524之状态压缩
- 状态压缩DP 入门
- 状态压缩DP入门
- 状态DP入门
- 状态压缩dp入门
- 状态压缩DP入门
- 状态压缩DP入门
- 状态压缩DP入门题
- 状态压缩DP入门题
- SOJ 3371 状态DP入门
- 状态dp若干入门题
- 状态压缩dp入门题
- 状态压缩DP入门题
- 状态压缩dp入门题目
- 状态压缩DP(入门)
- 【状态压缩dp 入门】POJ
- shiro-01基本权限控制
- node编程及Express框架搭建
- Android端App调用微信分享的具体流程记录
- C语言指针理解(一)
- python中np.multiply()、np.dot()和星号(*)三种乘法运算的区别
- 状态DP入门---poj3524
- mysql 数据类型
- 由FileInputStream的read()、read(byte[])两个函数的使用,重新认识ascii编码、Unicode编码有什么用?
- [Spring]Spring对JDBC的支持
- VIM skills
- poj3237 Tree
- shell 文件分割
- python2.x和python3.x中raw_input( )和input( )区别
- asc码即ascii,没有asc这一说,ascii后是ii而不是罗马数字