Corn Fields POJ - 3254(状压dp入门)

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

思路:

状压dp就是将一组数转化为一个数,以此来减少状态,比较重要的是合理运用位运算来解决状态的转移。

对于第i行的放置方法,它取决于本行的初始化状态和上一行的状态,这就是状态转移的条件。

dp[i][sta]+=dp[i-1][sta1](sta为即满足本行的初始状态又不与上一行冲突的状态)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
int n,m;
int state[maxn],top;//用sta来保存所有合法状态可以减少开dp的空间。类似于离散化的过程
int cur[100];//保存本行的初始状态
int dp[100][maxn];
inline bool check(int x)
{
    if(x&(x<<1)) return true;//检查是否有连续的1
    return false;
}
void init()
{
    top=0;
    int up=(1<<n);
    for(int i=0;i<up;i++)
    {
        if(check(i)==0)//记录每行合法的状态
        {
            state[++top]=i;
        }
    }
}
inline bool judge(int x,int k)
{
    if(x&cur[k]) return true;
    return false;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        init();
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++)
        {
            cur[i]=0;
            for(int j=1;j<=n;j++)
            {
                int x;
                scanf("%d",&x);
                if(x==0)//将有0的置为1,这样是为了便于检查初始化状态
                {
                    cur[i]|=(1<<(n-j));//利用位运算
                }
            }
        }
        for(int i=1;i<=top;i++)//初始化边界条件是状压的第一步
        {
            if(judge(state[i],1)==0)
            {
                dp[1][i]=1;
            }
        }
        for(int i=2;i<=m;i++)//根据状态转移进行递推
        {
            for(int k=1;k<=top;k++)
            {
                if(judge(state[k],i)) continue;
                for(int j=1;j<=top;j++)
                {
                    if(judge(state[j],i-1)) continue;
                    if(state[k]&state[j]) continue;
                    dp[i][k]=(dp[i][k]+dp[i-1][j])%mod;
                }
            }
        }
        int ans=0;//递推到最后一行即为答案
        for(int i=1;i<=top;i++)
        {
            ans=(ans+dp[m][i])%mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值