Poj 3254 corn fields (State compression DP)

Source: Internet
Author: User
Corn fields
Time limit:2000 ms   Memory limit:65536 K
Total submissions:4739   Accepted:2506

Description

Farmer John has purchased a lush New Rectangular pasture composedMByN(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. 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: MAnd N 
Lines 2 .. M+ 1: Line I+ 1 describes row IOf the pasture NSpace-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.

Farmer FJ has a rectangle of N rows and M columns. Some of the land is fertile and can be placed on the Land (expressed as 1 ), some land cannot put cattle (expressed as 0), and adjacent grids that can put cattle cannot have cattle at the same time, ask FJ about the total number of cow solutions (not a cow is also a solution ).

Analysis: Taking the example as an example, we can consider one row at a time. Assume that for each row, 1 is used to represent a cow, and 0 is used to represent a cow,

The status of the first line is: 000 001 010 011 (discard) 100 101 110 (discard) 111 (discard)

There are only five statuses that match the question, so there are five solutions in the first line.

The status of the second row is: 000 010

However, the 010 in the second line conflicts with the 010 in the first line. Therefore, if the status of the second line is 010, there are four solutions. If the status is 000, there are five solutions, therefore, there are 4 + 5 = 9 solutions.

After the analysis, we will find that each row can have a 01 string to indicate the status of this row, and this status can be replaced by a decimal integer, that is, this state is compressed into a decimal integer, so it is called State compression.

In this question, DP [I] [J] indicates the number of solutions when the status of row I is J.

State compression DP is the most common bitwise operation, because bitwise operations can easily determine whether the current state is legal. For example, in this question, if row I has two adjacent plots of land with cattle, assuming the current state is X, you only need to judge X & (x> 1) or whether the result of X & (x <1) is 0. If it is 0, it indicates that there is no adjacent. Otherwise, it indicates that there is adjacent.

 
#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 0x3f3f3f3fusing 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 (State compression DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.