poj3254(状压dp入门第一道题,很详细)

题目链接:http://poj.org/problem?id=3254

学习博客:https://blog.csdn.net/harrypoirot/article/details/23163485

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20591   Accepted: 10796

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 3
1 1 1
0 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.

Source

 
题目大意:农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑! )
 

解题思路:以样例数据第一行为例,三个格子都可以放牧,即每个格子都可以选择放,或不放。再考虑附加条件“相邻格子不可同时放牧”,那么我们可以列出单看第一行时的所有可行状态如下(1代表放牧,0代表不放牧)

编号 状态
1 0 0 0 
2 0 0 1
3 0 1 0
4 1 0 0
5 1 0 1
 
                                                                                                                            (表1)

由此,可将表中的状态看作二进制表示,那么,只需将每种状态转化为相应的十进制数,即可只用一个数字,就能表示某一种状态,如下表:

编号 二进制 十进制
1 0 0 0 0
2 0 0 1 1
3 0 1 0 2
4 1 0 0 4
5 1 0 1 5
                                                                                                                            (表2)
 
这种用一个数来表示一组数,以降低表示状态所需的维数的解题手段,就叫做状态压缩。

至此我们看到,在只考虑第一行的时候,有5种可行的放牧方案,但这只是我们要做的第一步。接下来要将第二行纳入考虑:

首先思考:纳入第二行后,会对当前问题造成什么样的影响?

答案还是那句话:“相邻格子不可同时放牧”!

也就是说,不止左右相邻不可以,上下之间也不能存在相邻的情况。

首先观察第二行,只有中间的格子可以放牧,那么我们的状态表格就可以相对简单些了~如下:

编号 二进制 十进制
1 0 0 0 0
2 0 1 0 2
                                                                                                                                          (表3)

只有两种可行状态,那么我们不妨一个一个来考察:

1、当第二行的状态为编号1时,第二行的三个格子都没有放牧,那么就不会与第一行的任何情况有冲突,第一行的5种方案都可行,即:第二行选用编号1的状态时,结合第一行,可得到5种可行的放牧方案;

2、当第二行的状态为编号2时,第二行中间的格子已经放牧了,那么第一行中间的格子就不可以放牧。看表2,发现其中第3种状态与当前第二行冲突,那么第一行只有4种方案是可行的,即:第二行选用编号2的状态时,结合第一行,可得到4种可行的放牧方案;

那么,在样例数据给出的情况下,我们的最终答案即为5+4=9;

通过对样例数据的分析即可以发现不同状态之间的关系:

以dp[i][state(j)]来表示对于前i行,第i行采用第j种状态时可以得到的可行方案总数!

例如:回头看样例数据,dp[2][1]即代表第二行使用第2中状态(0 1 0)时可得的方案数,即为4;

那么,可得出状态转移方程为:

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<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e8;
const int maxn=13;
const int maxm=1<<maxn;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int st[maxm],ma[maxm];//分别存取每一行可能的状态和给出的状态(用二进制中的第几位代表第几个数)
int dp[maxn][maxm];//表示在第i行状态为j时候可以放牛的种数
bool judge1(int x)//判断二进制有没有相邻的1
{
    return (x&(x<<1));//为什么要求这个呢?  因为只有二进制下没有相邻的1的数才有可能是一种放牧状态
}
bool judge2(int i,int x)
{
    return (ma[i]&st[x]);//如果有任意一位不能放,则这种情况是不能放的
}
int main()
{
    int n,m,x;
    scanf("%d%d",&n,&m);
    {
        memset(st,0,sizeof(st));
        memset(ma,0,sizeof(ma));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&x);
                if(x==0)
                    ma[i]+=(1<<(j-1));//(1<<j-1)意思是存储该位为1,代表该位不能放牧(从第0位开始)
            }
        }
        int k=0;
        for(int i=0;i<(1<<m);i++)//存储二进制下m位没有相邻1的数
        {
            if(!judge1(i))
                st[k++]=i;
        }
        for(int i=0;i<k;i++)//求第一行的状态,也可以当做是一种初始化,因为只有左右会影响
        {
            if(!judge2(1,i))
            {
                dp[1][i]=1;
            }
        }
        for(int i=2;i<=n;i++)//从第二行开始dp转移
        {
            for(int j=0;j<k;j++)
            {
                if(judge2(i,j))//判断第i行,如果按照状态j放牛行不行
                    continue;
                for(int f=0;f<k;f++)
                {
                    if(judge2(i-1,f))//剪枝,判断上一行的状态是否满足条件
                        continue;
                    if(!(st[j]&st[f]))//这一行的状态st[j]与上一行的状态st[f]没有任何相邻的
                        dp[i][j]+=dp[i-1][f];//这一行的等于上一行
                }
            }
        }
        int ans=0;
        for(int i=0;i<k;i++)
        {
            ans=(ans+dp[n][i])%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/9732514.html