POJ 3254(经典状压DP->求可以放置的方法数)@

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13642 Accepted: 7160

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.

题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法。


分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于第 i 行的可以放的一种情况,我们只要判断它和 i - 1 行的所有情况的能不能满足题目的所有牛不相邻,如果有种中满足,那么对于 i 行的这一中情况有 x 中放法。

前面分析可知满足子状态,我们我们确定可以用dp来解决。

但是我们又发现,状态是一种放法,不是我们平常dp的简单的状态,所以要用状态压缩!

但是什么是状态压缩呢?


比如前面的情况,一种放法是最多由12个 0 或者 1 组成的,那么我们很容易想到用二进制,用二进制的一个数来表示一种放法。

定义状态dp【i】【j】,第 i 行状态为 j 的时候放牛的种数。j 的话我们转化成二进制,从低位到高位依次 1 表示放牛0表示没有放牛,就可以表示一行所有的情况。

那么转移方程 dp【i】【j】=sum(dp【i-1】【k】)


状态压缩dp关键是处理好位运算。

这个题目用到了 & 这个运算符。

用 x & (x<<1)来判断一个数相邻两位是不是同时为1,假如同时为 1 则返回一个值,否则返回 0 ,这样就能优化掉一些状态

用 x & y 的布尔值来判断相同为是不是同时为1。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 14;
const int mod = 100000000;
typedef long long LL;
LL  dp[N][1<<N], s[1<<N], st[1<<N];


int main()
{
    int n, m, x;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        memset(s,0,sizeof(s));
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d", &x);
                if(!x)
                    s[i]|=(1<<j);
            }
        }
        int k=0;
        for(int i=0;i<(1<<m);i++)
        {
            if(!(i&(i<<1)))
                st[k++]=i;
        }
        for(int i=0;i<k;i++)
        {
            if(!(s[1]&st[i]))
                dp[1][i]=1;
        }
        for(int i=2;i<=n;i++)
        {
            for(int j=0;j<k;j++)
            {
                if(!(s[i]&st[j]))
                {
                    for(int p=0;p<k;p++)
                    {
                        if((st[p]&s[i-1])||(st[p]&st[j]))
                            continue;
                        dp[i][j]+=dp[i-1][p];
                    }
                }
            }
        }
        LL sum=0;
        for(int i=0;i<k;i++)
        {
            sum=(sum+dp[n][i])%mod;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值