[SGU]131. Hardwood floor

Analysis

    看到这么小的数据范围就想到是一道状压DP题。用二进制数表示每行的状态,用十进制存储,设f[i,opt]是第i行状态为opt的方案数,由于使用类似g[opt1,opt2]这样的数组来转移会MLE,所以只能DFS解决,不过效率还是很高的。

Accepted Code

var
    f:array[1..10,0..1000] of int64;
    i,j,n,m:longint;

procedure work(opt1,opt2,now:longint);
var
    k:longint;
begin
    if opt1=(1 shl m)-1 then
    begin
        f[i+1,opt2]:=f[i+1,opt2]+f[i,j];
        exit;
    end;
    for k:=now to m-1 do
        if (opt1 and (1 shl k)=0) then
        begin
            if (opt2 and (1 shl k)=0) then
                work(opt1 or (1 shl k),opt2 or (1 shl k),k+1);
            if (k>0) and (opt2 and (1 shl k)=0) and (opt2 and (1 shl (k-1))=0) then
                work(opt1 or (1 shl k),opt2 or (1 shl (k-1)) or (1 shl k),k+1);
            if k=m-1 then
                break;
            if (opt1 and (1 shl (k+1))=0) then
                work(opt1 or (1 shl k) or (1 shl (k+1)),opt2,k+2);
            if (opt2 and (1 shl k)=0) and (opt1 and (1 shl (k+1))=0) then
                work(opt1 or (1 shl k) or (1 shl (k+1)),opt2 or (1 shl k),k+2);
            if (opt2 and (1 shl k)=0) and (opt2 and (1 shl (k+1))=0) then
                work(opt1 or (1 shl k),opt2 or (1 shl k) or (1 shl (k+1)),k+1);
            if (opt2 and (1 shl (k+1))=0) and (opt1 and (1 shl (k+1))=0) then
                work(opt1 or (1 shl k) or (1 shl (k+1)),opt2 or (1 shl (k+1)),k+2);
            break;
        end;
end;

begin
    readln(n,m);
    fillchar(f,sizeof(f),0);
    f[1,0]:=1;
    for i:=1 to n do
        for j:=0 to (1 shl m)-1 do
            if f[i,j]>0 then
                work(j,0,0);
    writeln(f[n+1,0]);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值