思路:
先加入所有矩阵,然后每次扩散
c o d e code code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#define num(i, j) (i-1)*m+j
using namespace std;
int n, m, ma, mb, k, a[1100][1100];
long long sum[1100][1100];
priority_queue<pair<long long, pair<int, pair<int, pair<int, int> > > > > q;
map<pair<int, int>, int> hash;
int main()
{
scanf("%d%d%d%d%d", &n, &m, &ma, &mb, &k);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d", &a[i][j]), sum[i][j]=a[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
for(long long i=1; i<=n-ma+1; i++)
for(long long j=1; j<=m-mb+1; j++)
q.push(make_pair(-(sum[i+ma-1][j+mb-1]-sum[i+ma-1][j-1]-sum[i-1][j+mb-1]+sum[i-1][j-1]), make_pair(i, make_pair(j, make_pair(i+ma-1, j+mb-1)))));
long long s;
int x, y, x1, y1;
k--;
while(k--)
{
s=q.top().first, x=q.top().second.first, y=q.top().second.second.first, x1=q.top().second.second.second.first, y1=q.top().second.second.second.second;
q.pop();
if(x1<n&&!hash[make_pair(num(x, y), num(x1+1, y1))])
q.push(make_pair(-(sum[x1+1][y1]-sum[x-1][y1]-sum[x1+1][y-1]+sum[x-1][y-1]), make_pair(x, make_pair(y, make_pair(x1+1, y1))))), hash[make_pair(num(x, y), num(x1+1, y1))]=1;
if(y1<m&&!hash[make_pair(num(x, y), num(x1, y1+1))])
q.push(make_pair(-(sum[x1][y1+1]-sum[x-1][y1+1]-sum[x1][y-1]+sum[x-1][y-1]), make_pair(x, make_pair(y, make_pair(x1, y1+1))))), hash[make_pair(num(x, y), num(x1, y1+1))]=1;
}
printf("%lld", -q.top().first);
return 0;
}