您的位置:首页 > 其它

状压dp之二之三 炮兵阵地/玉米田 By cellur925

2018-08-02 21:46 134 查看

一、简单的状压dp 玉米田

题目描述

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的状态先进行预处理。而这道题的特点是给出土地的
贫瘠程度,不能种在贫瘠的地方。

在赋初值(第一行)时,我们也应检查第一行的各个状态是否满足贫瘠限制(开始并没有)。之后便像最简单的普通状压Dp一样,设
f[i][j]为当前行为第i行,当前行处在第j状态的方案数。转移方程有f[i][j]+=f[i-1][k],k枚举上一行状态。

转移前我们也应检查是否满足贫瘠限制(在这里费了大工夫233),最后的方法是写了一个pd函数。有些暴力地取出二进制的每一位
进行比较。然后判断条件时不要像互不侵犯一样判断8个格子!注意审题233!

code

1 #include<cstdio>
2 #include<algorithm>
3 #include<iostream>
4
5 using namespace std;
6
7 int n,m,cnt,ans;
8 int bin[2000],nowk[2000],pos[300];
9 int f[102][1030][1030];
10 char pam[150][20];
11
12 bool mout(int x,int y)
13 {//判断当前行的当前状态是否为山地不可放
14     if((x|pos[y])!=pos[y]) return 0;
15     return 1;
16 }
17
18 bool pd(int x,int y)
19 {//上下两行同列是否能攻击到对方
20     if(x&y) return 0;
21     return 1;
22 }
23
24 void getnowk(int x)
25 {
26     int tmp=0;
27     while(x) tmp+=(x&1),x>>=1;
28     nowk[cnt]=tmp;
29 }
30
31 void pre()
32 {
33     int fAKe=(1<<m)-1;
34     for(int i=0;i<fAKe;i++)
35      if((!(i&(i>>2)))&&(!(i&(i>>1)))) bin[++cnt]=i,getnowk(i);
36      //位运算优先级很低 需要大力加括号
37 }
38
39 int main()
40 {
41     scanf("%d%d",&n,&m);
42     pre();
43     for(int i=1;i<=n;i++) scanf("%s",pam[i]+1);//读入地图
44     for(int i=1;i<=n;i++)
45      for(int j=1;j<=m;j++)
46       if(pam[i][j]=='P') pos[i]=(pos[i]<<1)|1;
47        else pos[i]=pos[i]<<1;
48     //pos[i]记录第i行地图二进制压缩的01串转为的十进制数,1代表平地
49     //0代表山地,这个方法可以记住
50
51     for(int i=1;i<=cnt;i++)
52      f[1][i][0]=nowk[i];//预处理第一行
53
54     for(int i=2;i<=n;i++)
55      for(int j=1;j<=cnt;j++)//枚举当前行状态
56      {
57          int state_now=bin[j];
58          if(!mout(state_now,i)) continue;
59          for(int k=1;k<=cnt;k++)//枚举上一行状态
60          {
61              int state_last=bin[k];
62              if(!mout(state_last,i-1)) continue;
63              if(!pd(state_now,state_last)) continue;
64              if(i==2)//到第3行才能用前两行递推来,这里相当于一个特判
65              {
66                  f[i][j][k]=f[i-1][k][0]+nowk[j];
67                  continue;
68              }
69             for(int l=1;l<=cnt;l++)
70             {
71                 int state_ll=bin[l];
72                 if(!mout(state_ll,i-2)) continue;
73                 if(!pd(state_now,state_ll)) continue;
74                 if(!pd(state_last,state_ll)) continue;
75                 f[i][j][k]=max(f[i][j][k],f[i-1][k][l]+nowk[j]);
76             }
77         }
78      }
79
80     for(int i=1;i<=cnt;i++)
81      for(int j=1;j<=cnt;j++)
82       ans=max(ans,f
[i][j]);
83     printf("%d",ans);
84     return 0;
85 }
View Code

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: