poj 3254 状压dp
Description
Farmer John has purchased a lush new rectangular pasture composed of?M?by?N?(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. Canny FJ knows that 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:?M?and?NLines 2..?M+1: Line?i+1 describes row?i?of the pasture with?N?space-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 3 1 1 1 0 1 0Sample Output
9Hint
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. 轉自:http://blog.csdn.net/lenleaves/article/details/7974849 第一道狀態壓縮DP,雖然不難,但是。。。 遞推方程 dp[s][i] =Σ dp[s'][i-1]?? dp[s][i]表示第i行狀態為s時一共有多少種方案,從第一行開始向下遞推。 這里要注意 可以用一個數組先把每一行中符合要求的情況全部記錄,這樣可以有效減少狀態,和循環。另外題目要求我們取模,這里要注意。
?
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 10 #define N 15 11 #define M 15 12 #define mod 100000000 13 #define mod2 100000000 14 #define ll long long 15 #define maxi(a,b) (a)>(b)? (a) : (b) 16 #define mini(a,b) (a)<(b)? (a) : (b) 17 18 using namespace std; 19 20 int n,m; 21 int a[N]; 22 int dp[N][ (1<<12)+10]; 23 int ans; 24 25 void ini() 26 { 27 int i,j; 28 int re; 29 ans=0; 30 memset(dp,0,sizeof(dp)); 31 memset(a,0,sizeof(a)); 32 for(i=1;i<=n;i++){ 33 for(j=1;j<=m;j++){ 34 scanf("%d",&re); 35 a[i]=(a[i]<<1) + re; //把二進制壓縮成一個十進制數字 36 } 37 } 38 39 //for(i=1;i<=n;i++){ 40 // printf(" %d\n",a[i]); 41 //} 42 } 43 44 int ok(int i,int j) 45 { 46 if( (a[i] & j) !=j) return 0; //判斷下那一行種是否能夠找出flag這個狀態 47 while(j) 48 { 49 if(j%2==1 && (j/2)%2==1 ) return 0; 50 j/=2; 51 } 52 return 1; 53 } 54 55 void solve() 56 { 57 int i,j; 58 i=1; 59 for(j=0;j<=a[i];j++){ 60 if(ok(i,j)==0) continue; 61 dp[i][j]++; 62 } 63 for(i=2;i<=n;i++){ 64 for(j=0;j<=a[i];j++){ 65 if(ok(i,j)==0) continue; 66 for(int k=0;k<=a[i-1];k++){ 67 if(ok(i-1,k)==0) continue; 68 if( (j&k)!=0) continue; 69 dp[i][j]+=dp[i-1][k]; 70 dp[i][j]%=mod; 71 } 72 } 73 } 74 75 //for(i=1;i<=n;i++){ 76 // for(j=0;j<=a[i];j++){ 77 // printf(" %d %d dp=%d\n",i,j,dp[i][j]); 78 // } 79 // } 80 for(j=0;j<=a[n];j++){ 81 ans+=dp[n][j]; 82 ans%=mod; 83 //printf(" %d %d %d\n",j,dp[n][j],ans); 84 } 85 } 86 87 int main() 88 { 89 //freopen("data.in","r",stdin); 90 // scanf("%d",&T); 91 // for(int cnt=1;cnt<=T;cnt++) 92 //while(T--) 93 while(scanf("%d%d",&n,&m)!=EOF) 94 { 95 ini(); 96 solve(); 97 printf("%d\n",ans); 98 } 99 100 return 0; 101 } View Code?
轉載于:https://www.cnblogs.com/njczy2010/p/3930118.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的poj 3254 状压dp的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ALLEN-XIE
- 下一篇: 基于Fragment的百度地图框架的使用