码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3254 Corn Fields(状态压缩DP)

时间:2014-10-05 04:00:47      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   for   strong   

Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4739   Accepted: 2506

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.
 

题意:农夫FJ有一块n行m列的矩形土地, 有的土地是肥沃的,可以在这些土地上放牛(用1表示),有的土地不能放牛(用0表示),而且相邻的可以放牛的格子不能同时有牛,问FJ一共有多少种放牛的方案(一头牛都不放也是一种方案)。

分析:以样例为例,我们可以一行一行的考虑。假如对于每一行,用1表示放牛,0表示不放牛,

第一行的状态为:000  001 010  011(舍弃) 100 101 110(舍弃) 111(舍弃)

符合题意的状态只有5个,所以第一行有5种方案。

第二行的状态为:000 010

但是第二行中的010和第一行中的010冲突,所以如果第二行状态为010时,共有4种方案;状态为000时,有5种方案,所以一共有4+5=9种方案。

分析完我们会发现,对于每一行,都可以一个01串来表示这一行的状态,而这个状态可以用一个十进制整数来代替,也就是说,把这个状态压缩成了一个十进制整数,所以称为是状态压缩。

本题中,dp[i][j] 就表示第i行状态为j时的方案数。

状态压缩dp中最常见的就是位运算,因为位运算可以很方便的判断当前状态是否合法。例如本题中判断第i行是不是有两块相邻的土地同时都有牛,假设当前状态为X,那么只需要判断X&(X>>1)或者X&(X<<1)的结果是不是0,如果是0,说明没有相邻的,否则就说明有相邻的。

 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f

using namespace std;

int num[20][5006];
int dp[20][5006];

const int mod = 100000000;

int main()
{
    int N,M;
    while(scanf("%d%d",&M,&N)==2)
    {
        for(int i = 0;i<M;i++)
            for(int j = 0;j<N;j++)
        {
            scanf("%d",&num[i][j]);
        }
        M(dp,0);
        for(int i = 0;i<(1<<N);i++)
        {
            int flag = 1;
            for(int j = 0;j<N;j++)
            {
                if(i&(1<<j)&&num[0][j]==0||(i&(1<<j+1)&&(i&(1<<j))))
                {flag = 0; }
            }
            if(flag) {dp[0][i] = 1; //cout<<i<<endl;
            }
        }
        for(int x = 1;x<M;x++)
        {
            for(int i = 0;i<(1<<N);i++)
            {
                int flag = 1;
                for(int j = 0; j<N;j++)
                {
                    if(i&(1<<j)&&num[x][j]==0||(i&(1<<j+1)&&(i&(1<<j))))
                     {flag = 0; }
                }
                if(flag)
                {
                    for(int j = 0;j<(1<<N);j++)
                    {
                        int fg = 1;
                        for(int k = 0; k<N; k++)
                        {
                           if((i&(1<<k))&&(j&(1<<k)))
                           {fg = 0; }
                        }
                        if(fg)
                        {dp[x][i]= (dp[x][i]+dp[x-1][j])%mod;}
                    }
                }
            }
        }
        int ans = 0;
        for(int i = 0;i<(1<<N);i++)
        {
            ans = (ans+dp[M-1][i])%mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

 

POJ 3254 Corn Fields(状态压缩DP)

标签:des   style   blog   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/haohaooo/p/4006623.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!