(POJ 3254)Corn Fields <状态压缩DP 好题>

Corn Fields
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

USACO 2006 November Gold

题意:
有n*m大的一个地方,1表示土地肥沃可以种植物,0表示不能种植物,问:在不许有两个植物相邻的情况下,有多少种放置的方法。

分析:
刚开始做状压DP的题,对于这题而言我只能想到超时的DFS。
看了这边博客才搞懂的:http://blog.csdn.net/mengxiang000000/article/details/51075506
但是我也是啃了一个小时才完全搞懂。下面是我自己根据这篇博客做的思路整理

首先对于一个12*12的网格,每一行的选择可以有很多,为了方便表示出每一行选择的状态,我们将每一行压缩在一个int中,每一位二进制表示一个格子的状态(1,0)。
比如样例1变成了a[2] = {7,2}
然后题目要求没有两个所选的格子有相邻的边。我们从第一行开始往下依次选出每行可选的所有情况,那么我们只需要注意两点即可:(比如我们再选第i行)
1:第i行所选的格子一定是a[i]中二进制为1的位置的子集,并且没有1相邻。
2:第i行和第i-1行没有相邻的1被同时选中。

对于条件1:
判断(i,j)是否合法,首先我们知道,j的状态和第i行土地的状态的0的位子是一定相同的,但是j的1可以比土地的少,我们举例说明:
假设土地的状态值为5:1 0 1,我们合法的j放置状态有: 1 0 0 / 1 0 1 /0 0 1
对于土地状态我们对合法j放置状态进行&运算有:

  1 0 1
& 1 0 0
----------------
  1 0 0 

  1 0 1
& 1 0 1
------------------
  1 0 1

  1 0 1
& 0 0 1
-----------------
  0 0 1

我们发现,如果是合法的放置状态,我们用土地的状态值&j的状态值的结果一定等于j。辣么我们对于j是否合法的第一个判断就要这样写(反例大家随便写一个就发现确实不等于j):
if(a[i]&j!=j)return 0;
对于另外一个需要判断的条件:同一行不能有两个相邻的1,解决的方法就是在2进制01串的末尾加上一个0之后和原来的01串进行&运算,如果结果为0,合法,否则不合法,
我们也举例说明:

       1 0 1
   & 1 0 1 0
---------------------
     0 0 0 0合法
    1 1 0
& 1 1 0 0
----------------------
  0 1 0 0不合法

对于这两个判断我们已经了解如何操作,然后我们再用代码来实现:

int judge(int x,int y)
{
    if((a[x] & y) != y) return 0; // 判断是第i行可取的子集
    if((y & (y<<1)) != 0) return 0; //判断没有相邻的1
    return 1;
}

对于条件2:
只要 (j & k) == 0 那么就满足条件

对于所有满足条件的k我们就知道从i-1行的k状态可以到达第i行的j状态,即dp[i-1][k] ->dp[i][j],我们记录所有到达dp[i][j]状态的数目。那么dp[n][i]的和即为我们所要求的解。

对于样例dp[][]的所有状态如下:
这里写图片描述

整体dp思路和01背包一样

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define mod 100000000
int n,m,a[15];
int dp[13][1<<13];

int judge(int x,int y)
{
    if((a[x] & y) != y) return 0; // 判断是第i行可取的子集
    if((y & (y<<1)) != 0) return 0; //判断没有相邻的1
    return 1;
}

void solve()
{
    memset(dp,0,sizeof(dp));
    dp[0][0] = 1;  // 初始化
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<(1<<m);j++) //判断在第i行放置j这种状态的牛是否合法
        {
            if(judge(i,j)==0) continue;
            for(int k=0;k<(1<<m);k++) //找出所有第i-1行和第i行不冲突的状态
            {
                if((j&k) != 0) continue; //判断是否冲突
                dp[i][j] += dp[i-1][k];
                dp[i][j] %= mod;
            }
        }
    }
    int ans = 0;
    for(int i=0;i<(1<<m);i++)
    {
        ans += dp[n][i];
        ans %= mod;
    }
    printf("%d\n",ans);
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int k;
        for(int i=1;i<=n;i++)
        {
            a[i] = 0;
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&k);
                a[i] = (a[i]<<1) + k; //转化为2进制表示
            }
        }
        solve();
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值