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
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)
Output
Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.
Sample Input
2 3
1 1 1
0 1 0
Sample Output
9
Hint
Number the squares as follows:
1 2 3
4
There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.
Source
题目大意:
给定一个大小为M * N的大小的矩阵土地,每块土地都有肥沃和不肥沃两种情况,只有肥沃的土地可以放牛。
放牛的土地要求其相邻的四块土地没有其他牛(即上下左右没有牛)。
求一共有多种放牛的情况。
分析:
每块土地可以用 0 和 1 来表示是否放牛,因此可以用一个 int 来压缩一行的状态。
通过压缩,我们可以用以下两种按位与操作来判断某行的状态是否合法。
- x & (x << 1) != 0 ① x表示放牛情况,某位为1代表有牛,结果为0表示某行状态x不会出现两头牛相邻的情况
- x & y != 0 ② x表示放牛情况,y表示土地肥沃情况,某位为0代表肥沃,结果为0表示某行的牛都在肥沃的土地上
通过这样表示,我们可以使用数组 dp[行数][1 << 列数] 来进行动态规划,其中第一维表示当前处理第 i 行,第二维表示该行状态为 j 的情况下可放养的情况数量,转移方程为:
dp[i][j] = sum { dp[i - 1][k] },(其中要求 j,k 符合位操作①,且 (j, k) 符合位操作②)
AC代码:
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#define ll long long
const int INF = 0x3f3f3f3f;
const int N = 12;
const int M = 1 << N;
const int MOD = 100000000;
int state[M], map[N], dp[N][M];
int islegal(int i)
{
if((i & (i << 1)) == 0) return 1;
return 0;
}
int isfertile(int row, int i)
{
if((map[row] & i) == 0) return 1;
return 0;
}
int main()
{
int n, m, tmp;
scanf("%d%d", &n, &m);
memset(state, 0, sizeof(state));
memset(map, 0, sizeof(state));
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
scanf("%d", &tmp);
if(!tmp)
{
map[i] += (1 << j);
}
}
}
int index = 0;
for(int i = 0; i < (1 << m); i++)
{
if(islegal(i))
state[index++] = i;
}
for(int i = 0; i < index; i++)
{
if(isfertile(0, state[i])) dp[0][i] = 1;
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j < index; j++)
{
if(isfertile(i, state[j]))
{
for(int k = 0; k < index; k++)
{
if(isfertile(i - 1, state[k]) && (state[j] & state[k]) == 0) dp[i][j] += dp[i - 1][k];
}
}
}
}
int res = 0;
for(int i = 0; i < index; i++)
{
res = (res + dp[n - 1][i]) % MOD;
}
printf("%d\n", res);
return 0;
}