码迷,mamicode.com
首页 > 其他好文 > 详细

[USACO06NOV]Corn Fields(状压DP)

时间:2020-03-29 09:13:01      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:osi   The   空格   amp   Plan   ble   printf   inf   hose   

 

https://www.luogu.com.cn/problem/P1879

 

题目描述

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

 

 

很明显,这是一个状压dp

1、先预处理第i行的草地状态,压缩为一个整数line[i]。

2、再预处理所有状态中不相邻的状态can[i],每行共有(1<<m)-1种状态,但是很多是存在相邻的情况,所以提前处理不合法的状态可以在状态转移中降低很多时间复杂度

那么怎么判断某一状态是否相邻呢?只需要(i&(i<<1)==0)&&(i&(i>>1)==0)就可以判断,如果存在相邻情况则将can[i]置为false,不存在相邻则为true。

3、怎么处理只能放到肥沃的草地呢?对于第i行的地形line[i]和某一状态j,如果枚举到的line[i]&j==j即说明了两个状态完全相同,没有放到贫瘠草地的情况。

4、对于第i行不和i-1行有连通草地,枚举上一行方案k,该行方案j&k==0即可满足条件。

 

 

 

 1 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 const int INF=0x3f3f3f3f;
 4 const double eps =1e-8;
 5 const int mod=1e8;
 6 const int maxn=1e5+10;
 7 using namespace std;
 8 
 9 int G[15][15];
10 int line[15];//第i行的土地状态,相当于把a[i][j]中的第i行中的0,1拼接在一起 
11 int dp[15][1<<12+5];//到第i行时,状态为j的方案数 
12 int can[1<<12+5];//can[i]记录i这个状态是否合法
13 int n,m;
14 
15 int main()
16 {
17     #ifdef DEBUG
18     freopen("sample.txt","r",stdin);
19     #endif
20     
21     scanf("%d %d",&n,&m);
22     for(int i=1;i<=n;i++)
23     {
24         for(int j=1;j<=m;j++)
25         {
26             scanf("%d",&G[i][j]);
27             line[i]=(line[i]<<1) + G[i][j];//求出每一行的状态 
28         }
29     }
30     for(int i=0;i<(1<<m);i++) //判断每个状态是否符合要求 
31         can[i]=!(i&(i<<1)) && !(i&(i>>1));
32     dp[0][0]=1; 
33     for(int i=1;i<=n;i++) //枚举每行
34     {
35         for(int j=0;j<(1<<m);j++)//枚举这行每个可能的状态
36         {
37             if(can[j]&&((j&line[i])==j))//这行的状态合格,状态j与line[i]相同是保证了在肥沃土地上种草
38             {
39                 for(int k=0;k<(1<<m);k++)
40                 {
41                     if((k&j)==0)//该行状态与上一行的状态中没有出现两个1在同一列的 
42                         dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
43                 }
44             }
45         }
46     }
47     int ans=0;
48     for(int i=0;i<(1<<m);i++)
49         ans=(ans+dp[n][i])%mod;
50     printf("%d\n",ans);
51     
52     return 0;
53 }

 

 

 

 

 

 

 

 

 

-

[USACO06NOV]Corn Fields(状压DP)

标签:osi   The   空格   amp   Plan   ble   printf   inf   hose   

原文地址:https://www.cnblogs.com/jiamian/p/12590539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!