思路:用一个二元组(i,j)来表示有下标为(i,j)的前缀和,那么又下标(i,j)的x*y的子矩阵的连续和就为ans = (i,j) - (i - x,j) - (i,j - y) + (i - x,j - y);
if (ans > max_num) max_num = ans;
两层循环完了后,最终结果就是ans;
Ps:一开始嫌弃第一种写法,结果第二种写法写丑了,wa了4发QAQ。。。
/*****************************************
Author :Crazy_AC(JamesQi)
Time :2015
File Name :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long ll;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dp[1010][1010];
int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int T,n,m,x,y;
cin >> T;
while(T--){
cin >> n >> m >> x >> y;//x->n,y->m
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j)
cin >> dp[i][j];
int max_num = 0;
for (int i = 1;i <= n;++i){
for (int j = 1;j <= m;++j){
dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
if (i >= x && j >= y){
int ans = dp[i][j] - dp[i - x][j] - dp[i][j - y] + dp[i - x][j - y];
if (ans > max_num) max_num = ans;
}
}
}
cout << max_num << endl;
}
return 0;
}
/*****************************************
Author :Crazy_AC(JamesQi)
Time :2015
File Name :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
/*
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long ll;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dp[1010][1010];
int a[1010][1010];
int n,m,x,y;
int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int T;
cin >> T;
while(T--){
cin >> n >> m >> x >> y;
for (int i = 1;i <= n;++i) for (int j = 1;j <= m;++j) cin >> a[i][j];
MEM(dp, 0);
for (int i = 1;i <= n;++i){
for (int j = 1;j <= m;++j){
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
if (i >= x && j >= y){
int ans = a[i][j] - a[i - x][j] - a[i][j - y] + a[i - x][j - y];
dp[i][j] = Get_Max(dp[i - 1][j],dp[i][j - 1]);
dp[i][j] = Get_Max(ans,dp[i][j]);
}
}
}
cout << dp[n][m] << endl;
}
return 0;
}
*/