poj3254 Corn Fields

来源:互联网 发布:中国在中东知乎 编辑:程序博客网 时间:2024/05/16 10:37
F - Corn Fields
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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 31 1 10 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入门,先搜出所有的状态数,一共不过400种,这样,就可以用二维数组dp[i][j]表示第i层第j种状态,这样我们可以发现状态转移方
程dp[i][j]=dp[i][j]+dp[i-1][所有的合法状态],这样最后,求个总合,就可以得到答案,题目还是很简单的!
#include <iostream>#include<stdio.h>#include<string.h>using namespace std;int allstate,state[400],statenum,map[12][12],dp[12][400],m,n,maxall;int statecan(int i, int j)//第i行,第j种状态{    int k;    for(k=0;k<m;k++)    {        if(((map[i][m-k-1]==0))&&(state[j]&(1<<k)))//如果有一个是        {            return 0;        }    }    return 1;}void init(){    int allnum=(1<<m)-1,i,j,temp;    bool flag ;    statenum=0;    for(i=0;i<=allnum;i++)//得到所有的状态数377    {        flag=true;        for(j=0;j<=m-1;j++)        {            if((i&(1<<j))&&(i&(1<<j+1)))            {                flag=false;                break;            }        }        if(flag)        {          state[statenum++]=i;        }    }    memset(dp,0,sizeof(dp));    maxall=0;    for(i=0;i<n;i++)        for(j=0;j<m;j++)        {            scanf("%d",&map[i][j]);//1表示能放,0表示不能放        }    for(i=0;i<statenum;i++)//第一行进行初始化    {        if(statecan(0,i))        dp[0][i]=1;    }}void makedp(){    int i,j,k;    for(i=1;i<n;i++)    {        for(j=0;j<statenum;j++)        {            if(statecan(i,j))            {                 for(k=0;k<statenum;k++)                {                    if(!(state[j]&state[k]))                    {                    dp[i][j]=dp[i][j]+dp[i-1][k];                    }                }            }        }    }}int main(){    __int64 re;    int i,j;    while(scanf("%d%d",&n,&m)!=EOF)    {        init();        makedp();        re=0;        for(i=0;i<statenum;i++)        {            re+=dp[n-1][i];        }        printf("%I64d\n",re%100000000);    }    return 0;}


原创粉丝点击