乍一看这题好难,根本不会;
直到看到1<=m<=2……
显然分类讨论dp,
很快想到这题的dp和poj2430相类似
m=2的时候f[i,j,k]表示到第i行用了j个矩阵结尾状态为k时最大权值和
k=0~4表示5种结尾可能的状态
转移很类似poj2430,这里就不赘述了
1 const inf=1000000007; 2 var f:array[0..110,0..110,0..5] of longint; 3 a:array[0..110,0..2] of longint; 4 ans,i,j,p,k,n,m:longint; 5 6 function max(a,b:longint):longint; 7 begin 8 if a>b then exit(a) else exit(b); 9 end; 10 11 begin 12 readln(n,m,k); 13 for i:=1 to n do 14 for j:=1 to m do 15 read(a[i,j]); 16 if m=1 then 17 begin 18 for i:=0 to k do 19 begin 20 f[1,i,0]:=-inf; 21 f[1,i,1]:=-inf; 22 end; 23 f[1,1,1]:=a[1,1]; 24 f[1,0,0]:=0; 25 for i:=2 to n do 26 begin 27 for j:=0 to k do 28 begin 29 f[i,j,0]:=max(f[i-1,j,1],f[i-1,j,0]); 30 if j=0 then f[i,j,1]:=-inf 31 else f[i,j,1]:=max(max(f[i-1,j,1],f[i-1,j-1,1]),f[i-1,j-1,0])+a[i,1]; 32 end; 33 end; 34 writeln(max(f[n,k,1],f[n,k,0])); 35 end 36 else begin 37 for i:=0 to k do 38 for j:=0 to 4 do 39 f[1,i,j]:=-inf; 40 f[1,0,0]:=0; 41 f[1,1,1]:=a[1,1]; 42 f[1,1,2]:=a[1,2]; 43 f[1,2,3]:=a[1,1]+a[1,2]; 44 f[1,1,4]:=a[1,1]+a[1,2]; 45 for i:=2 to n do 46 for j:=0 to k do 47 begin 48 f[i,j,0]:=-inf; 49 for p:=0 to 4 do 50 f[i,j,0]:=max(f[i,j,0],f[i-1,j,p]); 51 52 if j=0 then 53 begin 54 for p:=0 to 4 do 55 f[i,j,p]:=-inf; 56 continue; 57 end; 58 f[i,j,1]:=f[i-1,j-1,0]; 59 f[i,j,1]:=max(f[i,j,1],max(f[i-1,j,1],f[i-1,j-1,1])); 60 f[i,j,1]:=max(f[i,j,1],max(f[i-1,j-1,2],f[i-1,j-1,4])); 61 f[i,j,1]:=max(f[i,j,1],max(f[i-1,j,3],f[i-1,j-1,3]))+a[i,1]; 62 63 f[i,j,2]:=f[i-1,j-1,0]; 64 f[i,j,2]:=max(f[i,j,2],max(f[i-1,j,2],f[i-1,j-1,2])); 65 f[i,j,2]:=max(f[i,j,2],max(f[i-1,j-1,1],f[i-1,j-1,4])); 66 f[i,j,2]:=max(f[i,j,2],max(f[i-1,j,3],f[i-1,j-1,3]))+a[i,2]; 67 68 f[i,j,4]:=max(f[i-1,j-1,4],f[i-1,j,4]); 69 for p:=0 to 3 do 70 f[i,j,4]:=max(f[i,j,4],f[i-1,j-1,p]); 71 f[i,j,4]:=f[i,j,4]+a[i,1]+a[i,2]; 72 73 if j<2 then 74 begin 75 f[i,j,3]:=-inf; 76 continue; 77 end; 78 f[i,j,3]:=f[i-1,j,3]; 79 f[i,j,3]:=max(f[i,j,3],max(f[i-1,j-2,0],f[i-1,j-2,4])); 80 f[i,j,3]:=max(f[i,j,3],f[i-1,j-2,3]); 81 f[i,j,3]:=max(f[i,j,3],max(f[i-1,j-1,1],f[i-1,j-1,2]))+a[i,1]+a[i,2]; 82 end; 83 ans:=-inf; 84 for i:=0 to 4 do 85 ans:=max(ans,f[n,k,i]); 86 writeln(ans); 87 end; 88 end.