Corn Fields(poj3254)状态压缩dp

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12738 Accepted: 6681

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

这题在好多大牛的博客里都被当做状态压缩dp的入门题目,然而看了好长时间大牛的题解才看懂O__O "…。
题目的意思是有一块m行n列的草地,1表示肥沃,0表示贫瘠,牛只能放在肥沃的草地上,而且要求所有的牛不能相邻,求共有多少种方法?
某一位置放与不放牛是一种状态,用状态压缩dp可以高效快速求解。但是我看到的题解比较坑的是,用Map[i]表示第i行给出地的状态,0表示肥沃,1表示贫瘠,这与题目不一样,所以看了好长时间才懂╮(╯_╰)╭。(自己亲自根据Map[i]+=1<<(j-1)模拟一下Map[i]
就知道了),所以Map[i]&state[x]为false时,表示第i行可以放置状态x(例如,样例中的第二行是0 1 0,而Map[2]则是101(二进制表示),101&010是false但是第二行是010可以放置状态010,所以 Map[i]&state[x]为false时,表示第i行可以放置状态x)。关于位运算又学到了一个姿势,判断一个正数x的二进制表示是否有相邻的1,可以用x&(x<<1)判断。这些是自己对题解的理解,下面是状态方程的建立。定义dp[i][j]:第i行放置状态j时的方法数,则dp[i][j]=∑dp[i-1][k],在利用递推式求解时需要判断第i行是否与上一行满足不存在相邻的1。大致思路就是这样,dp好难╮(╯_╰)╭。
AC代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=13;
const int M=1<<13;
const int Mod=1e8;
int dp[N][M];//dp[i][j]表示第i行状态为j的方法数
int Map[N];//每一行给出地的状态
int state[M];//状态
bool judge(int i,int x)//第i行是否可以放状态x,false可以,true不可以
{
    return (Map[i]&state[x]);
}
int main()
{
    memset(dp,0,sizeof(dp));
    memset(state,0,sizeof(state));
    memset(Map,0,sizeof(Map));
    int m,n,x;
    //m行n列
    scanf("%d%d",&m,&n);
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            scanf("%d",&x);
            if(x==0){
                Map[i]+=1<<(j-1);
            }
        }
    }
    int k=0;
    for(int i=0;i<(1<<n);i++){
        if(!(i&(i<<1))){
            state[k++]=i;
        }
    }
    //总共有k种状态满足条件:不存在相邻的1
    for(int i=0;i<k;i++){
        if(!judge(1,i)){
            dp[1][i]=1;
        }
    }
    for(int i=2;i<=m;i++){
        for(int j=0;j<k;j++){
            if(judge(i,j)) continue;//第i行不能按状态j放牛
            for(int f=0;f<k;f++){
                if(judge(i-1,f)) continue;//第i-1行不能按状态f放牛
                if(!(state[j]&state[f])){//判断第i行与第i-1行是否存在相邻的1
                    dp[i][j]+=dp[i-1][f];
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<k;i++){
        ans+=dp[m][i];
        ans%=Mod;
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值