[USACO06NOV]玉米田Corn Fields

题意:

给你一个n*m的矩阵,n,m<=12,其中一些地不可以种玉米,而且一块土地种了玉米后和他相邻的四块土地中也不可以种玉米,问你有多少种做法。(还可以全部不种)。

思路:

状态压缩dp,设f[i,j]为第i行,状态为j的时候的最大方案数。转移为
f[i,j]:=sum(f[i-1,k])其中k和j不会冲突

程序:

const
        maxn=1000;
        p=100000000;
var
        a,f,s:array [0..maxn,0..maxn] of longint;
        i,j,n,m,k,ans:longint;
        u:array [0..maxn] of longint;
procedure dfs(x,sum,o:longint);
var
        i,j:longint;
begin
        if o>m then exit;
        inc(u[x]);
        s[x,u[x]]:=sum;
        for i:=o+2 to m do
         if a[x,m-i+1]=1 then
          dfs(x,sum+1 shl (i-1),i);
end;

begin
        readln(n,m);
        for i:=1 to n do
        begin
         for j:=1 to m do
          read(a[i,j]);
         readln;
        end;
        for i:=1 to n do
         dfs(i,0,-1);
        for i:=1 to u[1] do
         f[1,i]:=1;
        for i:=2 to n do
         for j:=1 to u[i] do
          for k:=1 to u[i-1] do
           if  (s[i,j] and s[i-1,k])=0 then
            f[i,j]:=(f[i,j]+f[i-1,k]) mod p;
        for i:=1 to u[n] do
         ans:=(ans+f[n,i]) mod p;
        writeln(ans);
end.

你可能感兴趣的:(状压DP,usaco)