状压dp

针对集合的dp 用整数来表示状态

几种常用技巧

  • 判断第i为是否为1 (这个位是从右数起,从0开始。)if (x&(1<<i) {}或者if ((x>>i)&1) {}。 
  • 设置第i位为1 x |= 1<<i;
  • 设置第i位为0 x &= ~(1<<i);
  • 切换第i位 x ^= 1<<i;

poj3254 corn field

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.

将一行的可行性选出 存在state数组中 

 if(!(i & (i << 1))) 说明状态i没有种相邻的 是可行的

输入时将不能种的存为1 之后用&运算 一旦为1则表示 种在了不能种的地方

第一行的种植状态确定了以后 就可以用前一行判断后一行的种植状态

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<limits>
#include<stack>
#include<queue>
#include<cmath>
#define inf 1000005

using namespace std;

const int maxn = 20;
const int mod = 1e8;
const int maxm = 5000;
int m, n;
int dp[maxn][maxm];
int val[maxn], state[maxm];

int main()
{
    while(scanf("%d%d",&n,&m) != EOF){
        memset(dp,0,sizeof(dp));
        memset(val,0,sizeof(val));
        memset(state,0,sizeof(state));

        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                int a;
                scanf("%d",&a);
                if(a == 0){
                    val[i] += (1 << (m - j - 1));//记录每一行不能种的情况 置为1
                }
            }
        }

        int k = 0;
        for(int i = 0; i < (1 << m); i++){//从0到1<<m 枚举所有的状态 找到可行的状态
            if(!(i & (i << 1))) state[k++] = i;//i & (i << 1) == 0表示相邻不种的状态
        }
        for(int i = 0; i < k; i++){
            if(!(state[i] & val[0])) dp[0][i] = 1;//第0行种植i状态可行
            //若state[i] & val[0] == 1 说明state[i]状态有一个种在了unfertilized上
        }
        for(int i = 1; i <= n - 1; i++){
            for(int j = 0; j < k; j++){
                if(state[j] & val[i])
                    continue;
                for(int t = 0; t < k; t++){
                    if(state[t] & val[i - 1])
                        continue;//在上一行 t状态不可行
                    if(state[j] & state[t])
                        continue;//这一行j状态与上一行的状态有相邻 不可行
                    dp[i][j] = (dp[i][j] + dp[i - 1][t]) % mod;
                }
            }
        }

        for(int i = 1; i <= k - 1; i++){
            dp[n - 1][0] = (dp[n - 1][0] + dp[n - 1][i]) % mod;
        }
        printf("%d
",dp[n - 1][0]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wyboooo/p/9643454.html