POJ 3254 Corn Fields (state compression DP)

Source: Internet
Author: User

Description

Farmer John has purchased a lushNewRectangular pasture composed of M by N (1≤m≤ A;1≤n≤ A) square parcels. He wants to grow some yummy corn forThe cows on a number of squares. Regrettably, some of the squares is infertile and can't be planted. Canny FJ knows that the cows dislike eating close to all other, so when choosing which squares to plant, he avoids choosi Ng squares that is adjacent; No. 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 forHow to choose the squares forPlanting. He isSo open-minded that he considers choosing no squares asA valid option! Farmer John determine the number of ways he can choose the squares to plant.

Input

1: space-separated integers:m and    N2. m+1: Line i+1 are fertile (1for0 for infertile)

Output

1 It'sa.

Sample Input

2 3 1 1 1 0 1 0

Sample Output

9

Hint

Number The Squares asfollows:1 2 3    4There is four ways to plant only on one squares (1,2,3, or4), three ways to plant on both squares ( -, -, or the),1The plant on three squares (134), and one-to-plant on no squares.4+3+1+1=9.

Source

Usaco 2006 November Gold

Test instructions: Give an n row m row of grassland, 1 for fertile, 0 for barren, now to put some cows on the fertile grass, but requires all cows can not adjacent, ask you how many kinds of method.

Analysis: If we know all the i-1 of the line, then for the line I can be placed in a case, we just have to judge it and i-1 all the conditions of the row can not meet the problem of all the cows are not adjacent, if the kind of satisfied, then for I row of this case there is an X-in-place method.

The previous analysis shows that the sub-State is satisfied and we are sure we can use DP to solve it.

But we also found that the state is a method, not our usual DP of the simple state, so to use the state compression!

But what is state compression?

For example, in the previous case, a method is composed of up to 12 0 or 1, then it is easy to think of binary, with a binary number to represent a method of putting.

Defines the state DP "I" "J", the number of cattle cows when the first row state is J. J Words we turn into binary, from low to high 1 means cattle grazing 0 means that there is no cattle herding, it can represent a row of all cases.

So the transfer equation DP "I" "J" =sum (dp "I-1" "K")

The key of state compression DP is to handle good bit operation.

This problem uses the & operator.

Use X & (X<<1) to determine whether a number of adjacent two digits is 1, if at the same time 1 returns a value, otherwise return 0, so you can optimize the state of some

Use the Boolean value x & y to determine whether the same is 1 at the same time.

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineN 166 #defineM 1<<n7 #defineMOD 1000000008 intMP[M];//record each row, can not be planted on the status of 19 intSTATE[M];//record all states that are not adjacent to other numbers in 1<<mTen intDp[n][m]; One BOOLJudgeintx) { A     returnx& (x<<1); - } - BOOLJudge2 (intIintj) { the     returnmp[i]&State[j]; - } - intMain () - { +     intn,m; -      while(SCANF ("%d%d", &n,&m) = =2){ +  AMemset (MP,0,sizeof(MP)); atmemset (state,0,sizeof(state)); -Memset (DP,0,sizeof(DP)); -              -          for(intI=1; i<=n;i++){ -              for(intj=1; j<=m;j++){ -                 intx; inscanf"%d",&x); -                 if(x==0){ tomp[i]+= (1<< (J-1)); +                 } -             } the         } *  $         intk=0;Panax Notoginseng          for(intI=0;i< (1&LT;&LT;M); i++){ -             if(judge (i) = =0){ thestate[k++]=i; +             } A         } the  +          for(intI=0; i<k;i++){ -             if(Judge2 (1, i) = =0){ $dp[1][i]=1; $             } -         } -  the          for(intI=2; i<=n;i++){ -              for(intj=0; j<k;j++){Wuyi                 if(Judge2 (I,J))Continue; the                  for(intL=0; l<k;l++){ -                     if(Judge2 (i1, l))Continue; Wu                     if((state[j]&state[l]) = =0){ -dp[i][j]+=dp[i-1][l]; About                     } $                 } -             } -         } -         intans=0; A          for(intI=0; i<k;i++){ +ans=ans+Dp[n][i]; theans%=MOD; -         } $printf"%d\n", ans); the     } the     return 0; the}
View Code

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.