您的位置:首页 > 其它

玉米田(cowfood)

2016-07-22 20:46 190 查看
题意/Description:

       农民 John 购买了一处肥沃的矩形牧场,分成M*N(1 <= M <= 12; 1 <= N <= 12)个格子。他想在那里的一些格子中种植美味的玉米。遗憾的是,有些格子区域的土地是贫瘠的,不能耕种。精明的 FJ
知道奶牛们进食时不喜欢和别的牛相邻,所以一旦在一个格子中种植玉米,那么他就不会在相邻的格子中种植,即没有两个被选中的格子拥有公共边。他还没有最终确定哪些格子要选择种植玉米。

       作为一个思想开明的人,农民 John 希望考虑所有可行的选择格子种植方案。由于太开明,他还考虑一个格子都不选择的种植方案!请帮助农民 John 确定种植方案总数。

读入/Input:

   
   Line 1: 两个用空格分隔的整数 M 和 N

  Lines 2..M+1: 第 i+1 行描述牧场第i行每个格子的情况, N 个用空格分隔的整数,表示 这个格子是否可以种植(1 表示肥沃的、适合种植,0 表示贫瘠的、不可种植)

输出/Output:

 
     Line 1: 一个整数: FJ 可选择的方案总数 除以 100,000,000 的余数。

题解/solution:

 
     看到n,m<=12,可以考虑一下状压DP。

 
     因为M,N <=12 所以可以用一个longint来表示一行的状态。

       就可以用 f[i][nm] 表示第i行,状态是nm的时候的方案数。

       然后’2^12*2^12’枚举转移,判断一下是否可以转移。

       ∑dp
[i]为答案。

代码/Code:

var
n,m,nm,ans:longint;
a:array [0..13] of longint;
f:array [0..13,0..5001] of longint;
procedure init;
var
i,j,z:longint;
begin
readln(n,m);
for i:=1 to n do
for j:=1 to m do
begin
read(z);
a[i]:=a[i] shl 1+z;
end;
nm:=(1 shl m)-1;
end;

procedure main;
var
i,j,k:longint;
begin
for i:=0 to nm do
if ((i and (i shr 1))=0) and (i or a[1]=a[1]) then
f[1,i]:=1;
for i:=2 to n do
for j:=0 to nm do
if f[i-1,j]<>0 then
for k:=0 to nm do
if (j and k=0) and (k or a[i]=a[i]) and (k and (k shr 1)=0) then
f[i,k]:=(f[i,k]+f[i-1,j]) mod 100000000;
for i:=0 to nm do
ans:=(ans+f[n,i]) mod 100000000;
end;

begin
init;
main;
write(ans);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  玉米田cowfood