poj3254(状压dp)

Posted lmjer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3254(状压dp)相关的知识,希望对你有一定的参考价值。

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19518   Accepted: 10243

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.

题目应为状压dp入门题

设计好状态即可,方程并不难

依然把每行设计为状态,压缩到十进制数中,判断解是否可行即可

具体看代码

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

const int maxn=1<<12+10;

int dp[13][maxn];
int cur[13];
int ans[maxn];
int top;

int check(int x,int v){
    return !(ans[x]&cur[v]);
}

int main(){
    int n,m;
    int k;
    scanf("%d%d",&n,&m);
    for (int i=0;i<(1<<m);i++){
        if(!(i&(i<<1))) ans[++top]=i;将数左移一位后再与就可以判断原数是否01相间分布
    }
    for (int i=1;i<=n;i++)
         for (int j=1;j<=m;j++){
            scanf("%d",&k);
            if(!k) cur[i]+=(1<<(m-j));//因为图为 000111,而二进制数状态为从右到左,所以把其反过来,设不可行状态为1,就可以与以后状态比较,若与上值为真就说明状态不对
         }
    for (int i=1;i<=top;i++)
        if(check(i,1)) dp[1][i]=1;
    for (int i=2;i<=n;i++){
        for (int j=1;j<=top;j++){
            if(!check(j,i)) continue;
            for (int k=1;k<=top;k++){
                if(!check(k,i-1)) continue;
                 if(!(ans[j]&ans[k])) dp[i][j]=(dp[i][j]%100000000+dp[i-1][k]%100000000)%100000000;
            }
        }
    }
   int sum=0;
   for (int i=1;i<=top;i++) sum=(sum%100000000+dp[n][i]%100000000)%100000000;
   printf("%d
",sum%100000000);
return 0;
}

 





以上是关于poj3254(状压dp)的主要内容,如果未能解决你的问题,请参考以下文章

poj3254(状压dp)

poj3254(状压dp)

POJ 3254 Corn Fields(状压dp)

POJ 3254 & POJ 1185(状压DP入门)

POJ 3254 Corn Fields (状压dp)

POJ 3254 - Corn Fields - [状压DP水题]