现在的位置: 首页 > 综合 > 正文

POJ 3254 Corn Fields 状态压缩dp

2018年04月25日 ⁄ 综合 ⁄ 共 2282字 ⁄ 字号 评论关闭

A - Corn Fields

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

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,不清楚位运算的我刚开始一点思路都没有,咱们先来了解一下位运算

与运算&(两位都为1,才会为1)

m<<1左移一个位置

在题目中,判断一个状态是否两块连着是草地     状态m   1 0 0 1 1 0 1 0 0

  m<<1    0 0 1 1 0 1 0 0 0

可以清楚的看到做完运算后,结果不为0,由此判断此状态不合法

map[i]  |= (1<<j),将第j个数存入map[i](内部存储是以二进制形式)数组map存储的是我们所输入的每一行的状态

dp[i][j]//第i行时,可达到状态j的方案总数

现给出AC代码

#include<cstdio>
#include<string.h>
using namespace std;
const int MOD = 100000000;
int dp[15][1<<13];
int state[1<<13];
int map[15];
int index = 0;
int M,N;
bool istrue(int x)
{
    if(x & (x<<1))return false;
    return true;
}
void getstate(int x)//记录下所有的合法方案
{
    for(int i = 0; i <(1<<x); i++)
    {
        if(istrue(i))//判断是否合法
        {
           state[index++]=i;
        }

    }
}
int main()
{
    scanf("%d%d",&M,&N);
    memset(dp,0,sizeof(dp));
    memset(map,0,sizeof(map));
    getstate(N);
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
            int num;
            scanf("%d",&num);
            if(num == 0)
            {
                map[i] |= (1<<j);//向集合map中添加添加第j个元素
            }
        }
    }
    for(int i = 0; i < index; i++)
    {
        if(!(map[0]&state[i]))
            dp[0][i]=1;
    }
    for(int x = 1; x <M; x++)
    {
        for(int y = 0; y < index; y++)
        {
            if(map[x] & state[y])continue;
            for(int z = 0; z < index; z++)
            {
                if(state[y]&state[z])continue;
                dp[x][y] += dp[x-1][z];
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < index; i++)
    {
        ans += dp[M-1][i];
    }
    printf("%d\n",ans%MOD);
    return 0;
}

抱歉!评论已关闭.