POJ 3254 Corn Fields Country compression DP

Source: Internet
Author: User

Serie A champions:

To be in M row n Chocobo, there are some lattices you can plant trees that others cannot do. Not adjacent to the tree, I asked a number of different co-owned laws.

Analysis:

From the back to the next, the sub-problem extends to the parent problem, and when it is planted into a certain lattice, there is only the case of the n-1 lattice behind him and he has an effect on it. Therefore, the n lattice is encoded as a State s, indicating that the seed is finished (Domino that question is placed before. Note the difference, all feasible) the state of this n lattice. The parent problem is solved by a little Sub-problem, which is the idea of dynamic planning.

Code:

POJ 3254//sep9#include <iostream>using namespace std;const int Maxn=14;const int Mod=100000000;int land[maxN][ Maxn];int dp[2][1<<maxn];int M,n;int Main () {scanf ("%d%d", &m,&n); int i,j,used;for (i=0;i<m;++i) for ( J=0;J&LT;N;++J) scanf ("%d", &land[i][j]); int *cur=dp[0],*nxt=dp[1];cur[0]=1;for (i=m-1;i>=0;--i) for (j=n-1;j &GT;=0;--J) {for (used=0;used<1<<n;++used) {if (land[i][j]==0) {if (used>>j&1) nxt[used]=0;else{ Nxt[used]=cur[used];nxt[used]+=cur[used| ( 1&LT;&LT;J)];nxt[used]%=mod;}} Else{int res=0;if ((used>>j&1) && (used>> (j+1) &1)) {res=0;} else if (! ( used>>j&1) && (used>> (j+1) &1)) {res=cur[used];res+=cur[used| ( 1&LT;&LT;J)];} else if ((used>>j&1) &&! ( Used>> (j+1) &1) {res=cur[used& (~ (1&LT;&LT;J))];} else if (! ( USED&GT;&GT;J&AMP;1) &&! (Used>> (j+1) &1)) {res=cur[used];res+=cur[used| ( 1&LT;&LT;J)];} Nxt[used]=res%mod;}} Swap (CUR,NXT);} int sum=0;for (int i=0;i<1<<N;++i) {sum+=cur[i];sum%=mod;}  printf ("%d", sum); return 0;}


POJ 3254 Corn Fields Country 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.