POJ 3254 Corn Fields (状压DP)

Posted dwtfukgv

tags:

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

题意:给定一个n*m的01矩阵,然后求有多少种方式,在1上并且1不相邻。

析:一个简单的状压DP,dp[i][s] 表示 第 i 行状态为 s 时有多少种,然后只要处理不相邻就行了,比赛进位运算写错了一个地方。。。。。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 100000000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

LL dp[15][1<<12];
int val[15];

bool judge(int s){
  for(int i = 1; i < m; ++i)
    if((s&(1<<i)) && (s&(1<<i-1)))  return false;
  return true;
}

bool judge2(int i, int s){
  for(int j = 0; j < m; ++j)
    if((s&(1<<j)) && !(val[i]&(1<<j)))  return false;
  return true;
}

int main(){
  while(scanf("%d %d", &n, &m) == 2){
    memset(dp, 0, sizeof dp);
    memset(val, 0, sizeof val);
    for(int i = 1; i <= n; ++i)
      for(int j = 0; j < m; ++j){
        int x;
        scanf("%d", &x);
        if(x)  val[i] |= (1<<j);
      }

    int all = 1<<m;
    for(int i = 1; i <= n; ++i){
      for(int j = 0; j < all; ++j){
        if(!judge(j))  continue;
        if(!judge2(i, j))  continue;
        if(1 == i){ dp[i][j] = 1;  continue; }
        int t = 0;
        for(int k = 0; k < m; ++k)
          if(!(j&(1<<k)))  t |= 1<<k;
        dp[i][j] = dp[i-1][0];
        for(int k = t; k; k = (k-1)&t)
          if(judge(k)) dp[i][j] = (dp[i][j] + dp[i-1][k]) % mod;
      }
    }
    LL ans = 0;
    for(int i = 0; i < all; ++i)
      ans = (ans + dp[n][i]) % mod;
    cout << ans << endl;
  }
  return 0;
}

  

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

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

poj-3254 Corn Fields(状压dp)

POJ3254Corn Fields 状压dp

poj 3254 Corn Fields 状压DP

[poj 3254] Corn Fields 状压dp

POJ 3254 Corn Fields | 状压DP