状态压缩DP noi2001炮兵阵地

要注意影响两行就以最后两行为dp的状态

program cannon;
var n,m,i,j,l,tt,k,k1,aa,t,ans:longint;
    s,s1:array[-1..100]of longint;
    b:array[0..20]of byte;
    m2:array[0..10]of longint;
    ch:char;
    flag:byte;
    a:array[-1..100]of longint;
    f:array[-1..100,1..65,1..65]of longint;
function max(a,b:longint):longint;
begin
 if a>b then max:=a else max:=b;
end;
begin
//f[i,j,k]表示前i行,第i行状态为j,第i-1行状态为k的最大士兵数,枚举第i-2行
 readln(n,m);
 m2[0]:=1;
 for i:=1 to m do m2[i]:=m2[i-1]*2;
 for i:=1 to n do
  begin
   for j:=1 to m do
    begin
     read(ch);
     if ch='H' then a[i]:=a[i]+m2[m-j];
    end;
   readln;
  end;
 for i:=0 to 1 shl m-1 do
  begin
   aa:=i;
   l:=0;
   tt:=0;
   flag:=1;
   while aa>0 do
    begin
     l:=l+1;
     b[l]:=aa mod 2;
     if b[l]=1 then tt:=tt+1;
     if (b[l]=1)and((b[l-1]=1)or(b[l-2]=1)) then begin flag:=0;break;end;
     aa:=aa div 2;
    end;
   if flag=1 then
    begin
     t:=t+1;
     s[t]:=i;
     s1[t]:=tt;
    end;
  end;
 a[0]:=1 shl m-1;
 a[-1]:=1 shl m-1;
 for i:=1 to n do
  for j:=1 to t do
    if s[j] and a[i]=0 then
     for k:=1 to t do
       if s[k] and a[i-1]=0 then
        if s[j] and s[k]=0 then
         for k1:=1 to t do
           if (s[k1] and a[i-2]=0) then
            if (s[j] and s[k1]=0)and(s[k]and s[k1]=0) then
             f[i,j,k]:=max(f[i,j,k],f[i-1,k,k1]+s1[j]);
 ans:=0;
 for i:=1 to t do
  for j:=1 to t do
   if ans<f[n,i,j] then ans:=f[n,i,j];
 writeln(ans);
end.
             


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值