标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9806 | Accepted: 5185 |
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
Output
Sample Input
2 3 1 1 1 0 1 0
Sample Output
9
Hint
1 2 3
4
Source
1 #include <stdio.h> 2 #define MOD 100000000 3 int row[13], rec[377], dp[13][377]; 4 int main() 5 { 6 int M, N, t; 7 scanf("%d%d", &M, &N); 8 for(int i=0; i<M; i++) 9 for(int j=0; j<N; j++) 10 scanf("%d", &t), //t=M[i][j] 11 row[i]=(row[i]<<1)|!t; //reverse row state 12 int x=1<<12, k=0; 13 for(int i=0; i<x; i++) //calculate all valid states 14 if(!(i&(i<<1))) //is a valid row state 15 rec[k++]=i; 16 rec[k]=x; 17 x=1<<N; 18 for(int i=0; rec[i]<x; i++) //process first row 19 if(!(row[0]&rec[i])) 20 dp[0][i]=1; 21 for(int r=1; r<M; r++) //for each row 22 for(int i=0; rec[i]<x; i++) //for each valid state 23 if(!(row[r-1]&rec[i])) //if valid also for last row 24 for(int j=0; rec[j]<x; j++) //for each valid state 25 if(!(row[r]&rec[j])) //if valid also for this row 26 if(!(rec[i]&rec[j])) //and the two are not conflict 27 dp[r][j]=(dp[r][j]+dp[r-1][i])%MOD; 28 int r=M-1; 29 for(int i=1; rec[i]<x; i++) //process last row 30 dp[r][0]=(dp[r][0]+dp[r][i])%MOD; 31 printf("%d\n", dp[r][0]); 32 return 0; 33 }
POJ 3254. Corn Fields 状态压缩DP (入门级)
标签:
原文地址:http://www.cnblogs.com/BlackStorm/p/4706243.html