poj 3254Corn Fields

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

 
题目大意:给出一个n行m列的矩阵,其中的元素只有0或1。1的区域可以放牛,零的区域不能放牛,而且不能有两个相邻的区域同时放牛。问有多少种放牛的方法。
 
//===============================================================================================
 
这道题的范围只有12*12,2的12次方只有几千,longint可以承受,很明显用状态压缩动态规划,用二进制表示是否放牛,同时要判断是否是合法放牛方法。然后从前一行的可行状态转移。
 
AC CODE
 
program pku_3254;
const mo=100000000;
var f:array[0..1,0..8191] of longint;
      a:array[1..13] of longint;
      b:array[0..8191] of boolean;
      n,m,len,i,j,k,now,x,y,ans:longint;
begin
  readln(n,m); len:=1 shl m-1; a[1]:=-1;
  for i:=0 to len do
  begin inc(a[1]); j:=1;
      while a[j]>1 do
      begin a[j]:=0; inc(a[j+1]); inc(j); end;
      for j:=2 to m+1 do
          if (a[j]=1) and (a[j-1]=1) then break;
      if j>m then b[i]:=true else b[i]:=false;
  end;      //预处理出可行方案。
  for i:=1 to n do
  begin now:=0;
      for j:=1 to m do
      begin read(x);
          if x=0 then inc(now,1 shl (j-1));
      end; a[i]:=now;      //这里处理出a数组方便以后判断方案是否可行,会不会在不合法的地方放牛。
  end;
  y:=0; f[0,0]:=1;      //0的情况说明对于下一行没有限制。
  for i:=1 to n do
  begin
      x:=y; y:=x xor 1;
      for j:=0 to len do f[y,j]:=0;
      for j:=0 to len do
          if (j and a[i]=0) and b[j] then
              for k:=0 to len do
                  if j and k=0 then f[y,j]:=(f[x,k]+f[y,j]) mod mo;
  end;
  for i:=0 to len do ans:=(ans+f[y,i]) mod mo;
  writeln(ans);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值