POJ 3254 Corn fields (state DP primary)

Source: Internet
Author: User
Tags bitwise
Corn fields
Click to open the topic link
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8138 Accepted: 4339

Description

Farmer John has purchased a lush new rectangular pasture composed of M ByN (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 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 for what to choose the squares for Plantin G. He's so open-minded that he considers choosing no squares as a valid option! 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 are 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 is four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on both squares (, or), 1 WA Y to plant on three squares (134), and one-to-plant on no squares. 4+3+1+1=9.


The main topic: to give you a piece of grass, grass divided into small pieces of n*m, some of which can be grazing cattle, some can not, the topic given, the same small pieces can only put a bull, and the adjacent grassland put cattle of different types, text a total number of cows in the scheme. (Note the result mod1e8);

As for the status of DP, others say that if the range of n,m such as 32, etc., and so on, you can consider the state DP. I think that the state DP is the problem of the given conditions into each state, dynamic programming recursive solution. Of course, the difficulty is how to construct the state. Only by finding the right state can you skillfully apply this.

Due to the lack of familiarity with the bit operations, which led to a lot of WA, note (1<<m-1) and ((1<<m)-1) are not the same.
For this question, relatively simple (but I did a good long time, o__o "..., slag exploded). Just record the status of each row;
DP[I][K] Indicates the number of feasible schemes when the state of the first line is K, and the state transfer equation is dp[i][k]=∑ (dp[i-1][s]);
Code:
#include <iostream> #include <string.h> #include <stdio.h> #define N #define M 1<<13 #define MO
d 100000000 using namespace std;
int a[n];
Long Long dp[n][m]; int check1 (int x)///check to see if there is an adjacent same kind of cow {return! (x>>1) &x);//move Left or right after the bitwise AND} int check2 (int x,int y)//Determine whether the state of the two rows has the same type of {return}. X&AMP;Y);//bitwise AND} int check (int a,int x,int m)//Check that there is no need to put out {return!
    x& (~a));//Take a counter and x bitwise and} void Slove (int n,int m) {Long Long statue=1<<m;///2^m sub-state int i,j,k;
    Memset (Dp,0,sizeof (DP));
    for (i=0; i<=statue; i++)//Initialize {if (check (a[1],i,m) &&check1 (i)) dp[1][i]=1; } for (i=2, i<=n; i++) {for (j=0; j<=statue; J + +) {if (Check1 (j) &&check (a
                        [I],j,m)] {for (k=0; k<=statue; k++) {if (Check2 (j,k))
                dp[i][j]= (dp[i][j]%mod+dp[i-1][k]%mod)%mod;
      }      }}}} int main () {int n,m,i,j,x;
    Long long ans;
        while (~SCANF ("%d%d", &n,&m)) {ans=0;
        Long Long statue=1<<m;
                for (I=1; i<=n; i++) {for (j=1; j<=m; J + +) {scanf ("%d", &x);
        a[i]= (a[i]<<1) +x;///compressed to binary number}} slove (N,m);
        for (i=0; i<=statue; i++)//sum the number of the last line ans= (ans%mod+dp[n][i]%mod)%mod;
    printf ("%i64d\n", ans);
} return 0;
 }


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.