ACM: 动态规划题 poj 3254 学习状…

本文介绍了一种利用状态压缩动态规划方法解决牧地种植问题的技术方案。该问题要求在考虑土地肥沃性和避免相邻种植的情况下计算种植方式的数量。文章详细解释了状态压缩的概念及其应用,并给出了具体的实现代码。
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

 

题意: FJ要在放牧它的奶牛, 奶牛们不喜欢相邻在一起并且牧地有些地方时没有牧草的,

      要你求出放牧的方式有多少种.

 

解题思路:

     1. 一开始不知道怎么处理相邻间隔问题, 上一层和下一层错开分配, 但是每一层都会出现

        可以生长牧草和不可以生长牧草的区域.

     2. 网上发现一种方法: 状态压缩.

        对应题目: 每一层用0,1表示该区域是否能生长牧草. 用题目给出的例子.

        第一层全部为 1 1 1可以用二进制表示, 放牧的方式有:

        0 0 0(0), 0 0 1(1), 0 1 0(2), 1 0 0(4), 1 0 1(5) ==>5种方法.

        第二层0 1 0 的放牧方式:

        0 0 0(0), 0 1 0(2) ==> 2种.

        观察可以发现当第二层采用0 0 0(0)时, 第一层5种方法都可以采用.

                    当第二层采用0 1 0(2)时, 第一层的0 1 0(2)不可用, 即只有4种.

        即: 5+4 = 9种.

     3. 这样状态就容易多了, 设dp[i][j]: 第i-1层的放牧方式不和状态j(放牧方式)放生冲突

        的状态的和.

        状态方程: dp[i][j] = ∑(dp[i-1][k]) (0<= k <= (1<<m), m是二进制位数,n是层数)

        (方程要判断第i-1层的状态k不和j状态发出冲突.)

        结果: ∑dp[n][j]  (n>=1, 0<= j <= (1<<m) );

     4. 剩下问题是如何判断冲突:

        (1). 判断a和b是否有相同位: a & b

        (2). 判断a是否有相邻为都1: a & (a<<1);

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;
#define MAX 13
#define MOD 100000000

int n, m;
int a[MAX], dp[MAX][1<<MAX];

inline int adj(int a, int b)
{
 return !(a & b);
}

inline int bit(int a)
{
 return !(a & (a<<1));
}

int DP()
{
 int i, j, k;
 int sum, result = 0;
 for(i = 0; i < (1<<m); ++i)
 {
  if( adj(i, a[0]) && bit(i) )
   dp[0][i] = 1;
 }

 for(i = 1; i < n; ++i)
 {
  for(j = 0; j < (1<<m); ++j)
  {
   if( !adj(j, a[i]) || !bit(j) ) continue;
   sum = 0;
   for(k = 0; k < (1<<m); ++k)
   {
    if( dp[i-1][k] && adj(k, j))
    {
     sum += dp[i-1][k];
     sum %= MOD;
    }
   }
   dp[i][j] = sum;
  }
 }

 for(i = 0; i < (1<<m); ++i)
  result = (result + dp[n-1][i]) % MOD;
 return result;
}

int main()
{
 int i, j, t;
// freopen("input.txt", "r", stdin);
 while(scanf("%d %d",&n, &m) != EOF)
 {
  for(i = 0; i < n; ++i)
  {
   a[i] = 0;
   for(j = 0; j < m; ++j)
   {
    scanf("%d",&t);
    a[i] |= (!t) << j;
   }
  }
  
  memset(dp, 0, sizeof(dp));
  printf("%d\n", DP());
 }
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值