要注意影响两行就以最后两行为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.