【题解】 P1879 玉米田Corn Fields (动态规划,状态压缩)

题目描述

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.

农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地。John打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。

遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是John不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。

John想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)

输入输出格式

输入格式:

第一行:两个整数M和N,用空格隔开。

第2到第M+1行:每行包含N个用空格隔开的整数,描述了每块土地的状态。第i+1行描述了第i行的土地,所有整数均为0或1,是1的话,表示这块土地足够肥沃,0则表示这块土地不适合种草。

输出格式:

一个整数,即牧场分配总方案数除以100,000,000的余数。

输入输出样例

输入样例#1: 复制
2 3
1 1 1
0 1 0
输出样例#1: 复制
9

Solution:
  
一道标准动规题,由n<=12很容易联想到状压,如果定义dp[i][j]表示i,j这个点左上部分的矩阵的方案数,但是不好转移,并且状压没有体现。
  这时候我们可以想,状压压什么,可以直接压一行种草的位置,种草即为1,不种草即为0,这时候可以定义dp[i][k] i表示到第i行,状态为k时前面的状态
  转移方程就很容易想了,dp[i][k]是由上一行状态转移过来。
  简单来说:dp[i][k]=∑(k'满足情况) dp[i-1][k']
  k'需要满足什么情况呢:
    
1.满足输入矩阵规定的1与0,即判断每个位置种草是否合法
    2.满足不与上一行冲突,保证上一行状态为1的位置,这一行不为1,这就把两个状态&计算起来,即k&k' 若为0,即不冲突(可以好好理解下)

  然后一行一行dp计算就可以了

Code:
  
 1 //It is coded by Ning_Mew on 2.25
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include<algorithm>
 8 using namespace std;
 9 const int MOD=100000000;
10 int n,m,ans=0;
11 int a[15][15];
12 int num[15][15];
13 int dp[15][1<<13];
14 
15 void dfs(int x,int y){
16   if(y>m){x++;y=1;}
17   if(x==n+1&&y==1){ans++;ans%=MOD;return;}
18   if(a[x][y]){
19     if(num[x-1][y]||num[x][y-1]){
20       num[x][y]=0;dfs(x,y+1);
21     }
22     else{
23       num[x][y]=0;dfs(x,y+1);
24       num[x][y]=1;dfs(x,y+1);
25     }
26   }
27   else{num[x][y]=0;dfs(x,y+1);}
28   return;
29 }
30 bool check(int k,int l){
31   for(int i=1;i<=m;i++){
32     int ii=1+m-i;
33     //cout<<k<<' '<<ii<<' '<<((k>>(i-1))&1)<<endl;
34     if(a[l][ii]==0&&((k>>(ii-1))&1)==1)return false;
35   }
36   for(int i=1;i<=m;i++){
37     int ii=1+m-i;
38     if((k>>(ii-1)&1)==1&&((k>>ii)&1==1))return false;
39   }
40   return true;
41 }
42 int main(){
43   scanf("%d%d",&n,&m);
44   for(int i=1;i<=n;i++){
45     for(int j=1;j<=m;j++){scanf("%d",&a[i][j]);}
46   }
47   if(n<=3&&m<=3){
48     dfs(1,1);
49     printf("%d\n",ans);return 0;
50   }
51   
52   for(int i=0;i<=(1<<m)-1;i++){
53     if(check(i,1)){
54       //cout<<i<<endl;
55       dp[1][i]=1;
56     }
57   }
58   for(int i=2;i<=n;i++){
59     for(int k=0;k<=(1<<m)-1;k++){
60       if(!check(k,i))continue;
61       for(int kk=0;kk<=(1<<m)-1;kk++){
62     if((k&kk)==0){
63       dp[i][k]=dp[i][k]+dp[i-1][kk];
64       dp[i][k]=dp[i][k]%MOD;
65     }
66       }
67     }
68   }
69   for(int i=0;i<=(1<<m)-1;i++)ans=(ans+dp[n][i])%MOD;
70   printf("%d\n",ans);
71   return 0;
72 }

  转载附上本蒟蒻的链接和我说一声就ok了~  http://www.cnblogs.com/Ning-Mew/p/8469408.html

 
posted @ 2018-02-25 15:00  Ning_Mew  阅读(292)  评论(0编辑  收藏  举报