poj3254(状压dp)

Posted zhi-71

tags:

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

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20975   Accepted: 10998

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.
 
译文:(来着谷歌翻译)
 
 
玉米田

玉米田
时间限制:2000MS内存限制:65536K
提交总数:20975接受:10998
描述

农夫约翰购买了一个由M by N(1≤M≤12;1≤N≤12)方形包裹组成的郁郁葱葱的新长方形牧场。他想在一些广场上为奶牛种一些美味的玉米。遗憾的是,一些广场是不育的,不能种植。 Canny FJ知道奶牛不喜欢彼此吃东西,所以当选择哪个方块种植时,他避免选择相邻的方块;没有两个选中的正方形共享边缘。他还没有最终决定种植哪个广场。

作为一个思想开明的人,Farmer John想要考虑如何选择种植方块的所有可能选择。他是如此开放,他认为选择没有正方形是一个有效的选择!请帮助Farmer John确定他可以选择种植方块的方式。

输入

第1行:两个以空格分隔的整数:M和N.
第2..M + 1行:第i + 1行描述了牧场的第i行,其中N个以空格分隔的整数表示一个正方形是否为肥沃(1表示可育,0表示不育)
产量

第1行:一个整数:FJ可以选择模数为100,000,000的方格的数量。
样本输入

2 3
1 1 1
0 1 0
样本输出

9
暗示

将方块编号如下:
1 2 3
  4

有四种方法只在一个方格(1,2,3或4)上种植,三种方式种植在两个方格(13,14或34)上,1种方法种植在三个方格(134)上,一种没有广场的方式。 4 + 3 + 1 + 1 = 9。




#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<cstdio>
#include<map>
#include<set>
#include<string>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
inline ll gcd(ll i,ll j){
    return j==0?i:gcd(j,i%j);
}
inline ll lcm(ll i,ll j){
    return i/gcd(i,j)*j;
}
const int maxn=15;
const int mod=1000000000;
int n,m;
int cnt;
int now[maxn],state[1<<13],dp[maxn][1<<13];
inline void init(){
    int sum=1<<(m);
    for(int i=0;i<sum;i++){
        if(!(i&(i<<1)))
        state[++cnt]=i;
    }
    return ;
}
inline bool fit(int i,int j){
    return !(state[i]&now[j]);
}
int main(){
    int a,ans=0;
    cin>>n>>m;
    init();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a;
            if(a==0)
            now[i]+=1<<(m-j);//这里,要注意,可种玉米标记为0,不可种玉米标记为1
        }
    }
    for(int i=1;i<=cnt;i++){
        if(fit(i,1))
        dp[1][i]=1;
    }
    for(int i=2;i<=n;i++){
        for(int j=1;j<=cnt;j++){
            if(!fit(j,i))
            continue;
            for(int k=1;k<=cnt;k++){
                if(!fit(k,i-1))
                continue;
                if(state[j]&state[k])
                continue;
                dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
            }
        }
    }
    for(int i=1;i<=cnt;i++){
        ans=(ans+dp[n][i])%mod;
    }
    cout<<ans<<endl;
    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水题]