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.