poj-3254 Corn Fields (Getting started with state compression)

Source: Internet
Author: User

http://poj.org/problem?id=3254

Reference: http://blog.csdn.net/accry/article/details/6607703

Farmers want to grow corn on the land of M*n, but some land is very barren, so can not be planted, each land marked 1 of the expression of the species, marked 0 of the expression cannot be planted, and the land of corn can not be adjacent,

Ask how many kinds of legal planting programs there are. (Not all kinds also count as one)

The first form of pressure, understanding the relatively long time.

is to use the binary 0 and 1 to represent the land species or not, so that each row can be represented by a 2 decimal number, the number of columns <=12, so there are up to 2<<12 state.

Represents a state, it is possible to establish a state transition equation. dp[i][j] Represents the total number of scenarios for which the state of line I is J, Dp[i][j]=sigma (Dp[i-1][j ']);

The judgment conflict takes full advantage of the nature of the bitwise operation, such as whether a state has an adjacent 1 presence or a state x& (x>>1) or x& (x<<1). Because it is equal to move one bit to the left or to the right.

It is also the same as deciding whether to follow a line of conflict.

It is always wrong to write with a scrolling array, as if it were an initialization problem.

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> - //#include <map> the#include <queue> -#include <deque> - //#pragma COMMENT (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) +  - #definell Long Long + #defineINF 0x7f7f7f7f A #defineLC L,m,rt<<1 at #defineRC M + 1,r,rt<<1|1 - #definePi ACOs (-1.0) -  - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 in #defineMin (x, y) (x) < (y)? (x): (y) - #defineMax (x, y) (x) < (y)? (y): (x) to #defineE (x) (1 << (x)) + #defineIabs (x) (x) < 0? -(x): (x) - #defineOut (x) printf ("%i64d\n", X) the #defineLowbit (x) (x) & (-X) * #defineRead () freopen ("A.txt", "R", stdin) $ #defineWrite () freopen ("B.txt", "w", stdout);Panax Notoginseng #defineMAXN 110 - #defineMAXV 5010 the #defineMoD 1000000000 + using namespacestd; A intn,m,top=0; the intstate[ -],num[ the]; + intdp[ -][ -];//A maximum of 600 states, not knowing how it was calculated. - intcur[ -]; $InlineBOOLOkintX//determine if the same row has adjacent 1 $ { -     if(x&x<<1)return 0; -     return 1; the } - voidInit ()//Initializes a state of 2^m, removing the state of the adjacent 1Wuyi { thetop=0; -     intTotal=1<<m; Wu      for(intI=0; i<total;i++) -         if(OK (i)) state[++top]=i; About } $InlineBOOLFitintXintK//determine if the state X and read the K line conflict, note cur[k] 1 means not species, -{//so as long as the phase is 1, that's not true. -     if(X&cur[k])return 0; -     return 1; A } + intMain () the { -     //Read (); $      while(~SCANF ("%d%d",&n,&m)) the     { the init (); theMemset (DP,0,sizeof(DP)); the          for(intI=1; i<=n;i++) -         { incur[i]=0; the             intnum; the              for(intj=1; j<=m;j++)//here is for 0 to indicate can be planted, for 1 to indicate that is not to be planted About{//attention and the above distinction, here is mainly to judge the conflict. thescanf"%d",&num); the                 if(!num) cur[i]+= (1<< (m-j));//convert each row into 2 and store it with cur the             } +             //printf ("%d\n", Cur[i]); -         } the          for(intI=1; i<=top;i++)//initializes the first row,Bayi         { the             if(Fit (state[i),1))//No conflict means you can put thedp[1][i]=1; -         } -          for(intI=2; i<=n;i++) the         { the              for(intj=1; j<=top;j++) the             { the                 if(!fit (State[j],i))Continue;//Determine if line I and read-in graphs conflict -                  for(intk=1; k<=top;k++) the                 { the                     if(!fit (state[k],i-1))Continue;//Determine if line i-1 is conflicting the                     if(State[j]&state[k])Continue;//determine if row I and line i-1 conflict94Dp[i][j]= (dp[i][j]+dp[i-1][K])%MoD; the                 } the             } the         }98         intans=0; About          for(intI=1; i<=top;i++) -         {101Ans= (Ans+dp[n][i])%MoD;102         }103printf"%d\n", ans);104     } the     return 0;106}

poj-3254 Corn Fields (Getting started with state compression)

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.