「POJ3254」[USACO06NOV] 玉米田Corn Fields - 状压dp
Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20492 Accepted: 10738
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.
农场主 \(John\) 新买了一块长方形的新牧场,这块牧场被划分成 \(M\) 行 \(N\) 列(\(1 ≤ M ≤ 12\); \(1 ≤ N ≤ 12\)),每一格都是一块正方形的土地。 \(John\) 打算在牧场上的某几格里种上美味的草,供他的奶牛们享用。
遗憾的是,有些土地相当贫瘠,不能用来种草。并且,奶牛们喜欢独占一块草地的感觉,于是 \(John\) 不会选择两块相邻的土地,也就是说,没有哪两块草地有公共边。
\(John\) 想知道,如果不考虑草地的总块数,那么,一共有多少种种植方案可供他选择?(当然,把新牧场完全荒废也是一种方案)
Input
Line 1: Two space-separated integers: M and N
Lines 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)
第一行:两个整数 \(M\) 和 \(N\) ,用空格隔开。
第 \(2\) 到第 \(M+1\) 行:每行包含 \(N\) 个用空格隔开的整数,描述了每块土地的状态。第 \(i+1\) 行描述了第 \(i\) 行的土地,所有整数均为 \(0\) 或 \(1\) ,是 \(1\) 的话,表示这块土地足够肥沃, \(0\) 则表示这块土地不适合种草。
Output
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
一个整数,即牧场分配总方案数除以 \(100,000,000\) 的余数。
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
思路
数据范围很明显暗示了这个题要状压 \(dp\) 。
\(f[i][j]\) 表示第 \(i\) 行的第 \(j\) 个状态能被之前多少种情况转移出来。
我们定义一个 \(field\) 数组来表示第 \(i\) 行的草地情况, \(maxstate\) 表示 \(2^m\) 为最大方案种数,二进制表示为 \(111111111111\) ,再用 \(state\) 数组表示该解是否可行。
判断可行解的时候左移之后 \(and\) 一下再右移之后 \(and\) 一下,可以分别判断某值为 \(1\) 的一位的左边或右边紧挨着的一位是否也是 \(1\) 。
执行的时候先枚举行数 \(i\) 再枚举状态 \(j\) ,其中 \(((j\) & \(field[i])==j)\) 代表该方案与草地情况不冲突,再枚举上一层的方案 \(k\) , \(((k\) & \(j)==0)\) 代表没有两位同时为 \(1\) 的情况,也就是竖着没有相邻的草地,合法情况累加到下一层。
统计答案的时候要把第 \(n\) 层所有状态的转移数全部累加
代码
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<iostream>
#define rg register
using namespace std;
inline int read(){
rg int f=0,x=0;
rg char ch=getchar();
while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
const int N = 13;
const int mod = 1e8;
int f[N][(1<<(N-1))+1], field[N], n, m, maxstate, ans;
bool state[N];
signed main(){
n = read(),m = read();
maxstate = 1 << m;
for(rg int i = 1; i <= n; ++i)
for(rg int j = 1; j <= m; ++j)
field[i] = (field[i] << 1) + read();
for(rg int i = 0; i < maxstate; ++i)
state[i] = (((i << 1) & i) == 0) && (((i >> 1) & i) == 0);
f[0][0] = 1;
for(rg int i = 1; i <= n; ++i)
for(rg int j = 0; j < maxstate; ++j)
if(state[j] && (j & field[i]) == j)
for(rg int k = 0; k < maxstate; ++k)
if((k & j) == 0)
f[i][j] = (f[i][j] + f[i-1][k]) % mod;
for(rg int i = 0; i < maxstate; ++i)
ans = (ans + f[n][i]) % mod;
printf("%d",ans);
return 0;
}