【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=1177
【题目大意】
给出一个矩阵,从中选出3个k*k且不相交的矩阵,使得其总和最大
【题解】
只要处理四个方向的前缀最大值,就可以分类比较得到答案。
【代码】
#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define red(i,a,b) for(int i=b;i>=a;i--)
const int N=2600;
int n,m,k,x,s[N][N],a[N][N],b[N][N],c[N][N],d[N][N],ans=0;
int main(){
scanf("%d%d%d",&n,&m,&k);
rep(i,1,n)rep(j,1,m){scanf("%d",&x);s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+x;}
red(i,k,n)red(j,k,m)s[i][j]-=s[i-k][j]+s[i][j-k]-s[i-k][j-k];
rep(i,k,n)rep(j,k,m)a[i][j]=max(s[i][j],max(a[i-1][j],a[i][j-1]));
rep(i,k,n)red(j,k,m)b[i][j]=max(s[i][j],max(b[i-1][j],b[i][j+1]));
red(i,k,n)rep(j,k,m)c[i][j]=max(s[i][j],max(c[i+1][j],c[i][j-1]));
red(i,k,n)red(j,k,m)d[i][j]=max(s[i][j],max(d[i+1][j],d[i][j+1]));
rep(i,k,n-k)rep(j,k,m-k)ans=max(ans,a[i][j]+b[i][j+k]+c[i+k][m]);
rep(i,k,n-k)rep(j,k+k,m)ans=max(ans,b[i][j]+d[i+k][j]+a[n][j-k]);
rep(i,k+k,n)rep(j,k,m-k)ans=max(ans,c[i][j]+d[i][j+k]+a[i-k][m]);
rep(i,k,n-k)rep(j,k,m-k)ans=max(ans,a[i][j]+c[i+k][j]+b[n][j+k]);
rep(i,k,n)rep(j,k+k,m-k)ans=max(ans,s[i][j]+a[n][j-k]+b[n][j+k]);
rep(i,k+k,n-k)rep(j,k,m)ans=max(ans,s[i][j]+a[i-k][m]+c[i+k][m]);
printf("%d\n",ans);
}