状态压缩入门(Poj3254)

题目:

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.

 

参考:

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

编号状态
10 0 0 
20 0 1
30 1 0
41 0 0
51 0 1

(表1)

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

编号二进制十进制
10 0 00
20 0 11
30 1 02
41 0 04
51 0 15

(表2)

这种用一个数来表示一组数,以降低表示状态所需的维数的解题手段,就叫做状态压缩。

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

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

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

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

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

编号二进制十进制
10 0 00
20 1 02

(表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 <stdio.h>
#include <string.h>
#include <algorithm>
#define mod 100000000
#define LL long long

using namespace std;

LL dp[20][600];
int cur[20];
int a[600];
bool check(int x)//判断他的相邻格子是否为1;如果是1,就不符合;否则符合。
{
    if(x&(x << 1)) return false;
    return true;
}
bool cmp(int x,int i)//判断x状态与第i行的逆状态是否相符。
{
    if(x&cur[i]) return false;//如果一样返回false,否则返回true
    return true;//因为存储的逆状态,所以如果&的结果相同,就是与逆状态相同,与原状态就不同,所以可以选。
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(dp,0,sizeof dp);
        int tot = 0;
        for(int i = 0;i < 1<<m;i ++)//枚举出每一行的所有状态。
            if(check(i))a[++tot] = i;//如果符合情况,就存起来
        for(int i = 1;i <= n;i ++)
        {
            cur[i] = 0;
            int num;
            for(int j = 1;j <= m;j ++)
            {
                scanf("%d",&num);
                if(num == 0)
                cur[i] += 1<<(m - j);//每一行都按照其逆状态存储,所以cmp才会那样写。
            }

        }
        for(int i = 1;i <= tot;i ++)//找出和第一行相同的所有情况。
            if(cmp(a[i],1))
            dp[1][i] = 1;
        for(int i = 2;i <= n;i ++)
            for(int j = 1;j <= tot;j ++)
            {
                if(!cmp(a[j],i))continue;//求出第i行的状态。如果他和逆状态相同,就和原状态不同,因此就需要剪枝。
                for(int k = 1;k <= tot;k ++)
                {
                    if(!cmp(a[k],i-1))continue;//同上。
                    if(a[j]&a[k])continue;
                    dp[i][j] = (dp[i][j] + dp[i-1][k])%mod;//dp转移方程。
                }
            }
            int ans = 0;
            for(int i = 1;i <= tot;i ++)
                ans += dp[n][i];//求出第n行的每一种状态的方案和。
            printf("%d\n",ans % mod);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值