POJ3254 Corn Fields

题目

Description

2000MS/65536K
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.

Simple input

2 3
1 1 1
0 1 0

Simple output

9

题目分析

题目大意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),农夫可以在这些格子里放牛,其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。现在输入数据给出这块地的大小及可否放牧的情况,求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!

解题思路 :以样例数据第一行为例,三个格子都可以放牧,即每个格子都可以选择放,或不放。再考虑附加条件“相邻格子不可同时放牧”,那么我们可以列出单看第一行时的所有可行状态如下(1代表放牧,0代表不放牧)

编号 状态
1 0 0 0
2 0 0 1
3 0 1 0
4 1 0 0
5 1 0 1
(表1)
由此,可将表中的状态看作二进制表示,那么,只需将每种状态转化为相应的十进制数,即可只用一个数字,就能表示某一种状态,如下表:

编号 二进制 十进制
1 0 0 0 0
2 0 0 1 1
3 0 1 0 2
4 1 0 0 4
5 1 0 1 5
(表2)
这种用一个数来表示一组数,以降低表示状态所需的维数的解题手段,就叫做状态压缩。
至此我们看到,在只考虑第一行的时候,有5种可行的放牧方案,但这只是我们要做的第一步。接下来要将第二行纳入考虑:

首先思考:纳入第二行后,会对当前问题造成什么样的影响?

答案还是那句话:“ 相邻格子不可同时放牧 ”!

也就是说,不止左右相邻不可以,上下之间也不能存在相邻的情况。

首先观察第二行,只有中间的格子可以放牧,那么我们的状态表格就可以相对简单些了~如下:

编号 二进制 十进制
1 0 0 0 0
2 0 1 0 2
(表3)
只有两种可行状态,那么我们不妨一个一个来考察:
1、当第二行的状态为编号1时,第二行的三个格子都没有放牧,那么就不会与第一行的任何情况有冲突,第一行的5种方案都可行,即:第二行选用编号1的状态时,结合第一行,可得到5种可行的放牧方案;

2、当第二行的状态为编号2时,第二行中间的格子已经放牧了,那么第一行中间的格子就不可以放牧。看表2,发现其中第3种状态与当前第二行冲突,那么第一行只有4种方案是可行的,即:第二行选用编号2的状态时,结合第一行,可得到4种可行的放牧方案;

那么,在样例数据给出的情况下,我们的最终答案即为5+4=9;

通过对样例数据的分析即可以发现不同状态之间的关系:

以 dp[i][state(j)] 来表示对于 前i行 , 第i行 采用 第j种状态 时可以得到的 可行方案总数!

例如:回头看样例数据,dp[2][1]即代表第二行使用第2中状态(0 1 0)时可得的方案数,即为4;

那么,可得出状态转移方程为:

dp[i][state(j)]=dp[i-1][state(k1)]+dp[i-1][state(k2)]+……+dp[i-1][state(kn)] (kn即为上一行可行状态的编号,上一行共有n种可行状态)

最终ans=dp[m][state(k1)]+dp[m][state(k2)]+……+dp[m][state(kn)]; (kn即为 最后一 行 (第m行) 可行状态的编号)
http://www.tuicool.com/articles/JVzMVj

AC代码

924K/16MS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
void fre()
{

freopen("c://test//input.in", "r", stdin);
freopen("c://test//output.out", "w", stdout);
}
#define MP(x,y) make_pair(x,y)
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
#define debug puts("----------")
#define inf 0x3f3f
#define maxn 1050
const long long mod = 100000000;
using namespace std;
const int N = 13;
const int M = 1<<N;
int dp[N][M];
int mapp[M],st[M];
bool judge1(int x)//横向判断判断该行是否能够成立
{

return (x&(x<<1));
}
bool judge2(int x,int n)//判断x,n两行有没有重复的情况
{

return (mapp[x]&st[n]);
}
/*
此处,注意要用相反存储的数据来判断,
因为若10101001是一种可行状态,则可知101001也可行(是前者的一部分)
这时x即为10101001,cur[k]为10110,x&cur[k]=0,即符合条件
*/

int main()
{

//int x = 3;
//printf("%d %d %d\n",x,x<<1,x&x<<1);
int n,m,x;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(mapp,0,sizeof(mapp));
memset(st,0,sizeof(st));
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=m; j++)
{
scanf("%d",&x);
if(x==0)
mapp[i]+=(1<<(j-1));///反向存储,将该位置置为1
}
}

int k = 0;
for(int i=0; i<(1<<m); i++)
{
if(!judge1(i))//可行,则增添一种可能性
st[k++]=i;
}
for(int i=0; i<k; i++)
{
if(!judge2(1,i))//判断第i种状态与第一行是否重合
dp[1][i]=1;
}
// for(int i=0;i<k;i++)
// {
// printf("%d\n",dp[i][1]);
// }
for(int i=2; i<=n; i++)
{
for(int j=0; j<k; j++)
{
if(judge2(i,j))
continue;
for(int f=0; f<k; f++)
{
if(judge2(i-1,f))//纵向判断两行情况
continue;
if(!(st[j]&st[f]))//横向判断情况数目
dp[i][j]+=dp[i-1][f];
}
}
}
int ans=0;
for(int i=0; i<k; i++)
{
//printf("%d + \n",dp[n][i]);
ans+=dp[n][i];
ans%=mod;
}
printf("%d\n",ans);
}
}

题目链接

http://poj.org/problem?id=3254