The largest sub-matrix hdu1559
Problem Description give you a m × n matrix of integers, to find a submatrix x × y In the above, the maximum of the sub-matrix and the all elements.
Input data to the first input acts a positive integer T, represents a group T-test data. A first set of test data for each behavior four positive integers m, n-, X, Y (0 <m, n-<1000
the AND 0 <X <= m the AND 0 <Y <= n-), represents a given rectangle has m rows and n columns. Then this matrix with m rows each of n is a positive integer not greater than 1000.
Output
For each test, output an integer, and represents the maximum of the sub-matrix.
Sample Input
1
4 5 2 2
3 361 649 676 588
992 762 156 993 169
662 34 638 89 543
525 165 254 809 280
Sample Output
2474
Solution to a problem as follows
#include<iostream>
using namespace std;
const int Len = 1005;
// int map[Len][Len];
int dp[Len][Len]; //⚠️这里把存放数值的map数组,与dp 合并到一起使用了
int main()
{
// freopen("test_3.txt","r",stdin);
int t;
scanf("%d",&t);
while(t --)
{
int m,n,x,y;
scanf("%d %d %d %d",&m,&n,&x,&y);
for(int i = 1;i <= m;i ++)
for(int j = 1;j <= n;j ++)
{
scanf("%d",&dp[i][j]);
dp[i][j] = dp[i][j] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
}
int ans = 0;
for(int i = 1;i <= m - x;i ++)
for(int j = 1;j <= n - y;j ++)
{
ans = max(ans , dp[i + x - 1][j + y - 1] - dp[i - 1][j + y - 1] - dp[i + x - 1][j - 1] + dp[i - 1][j - 1]);
//ans = max(ans , dp[i + x][j + y] - dp[i][j + y] - dp[i + x][j] + dp[i][j]); //⚠️下面这种错误情况,减去的dp的下标范围不对
}
cout<<ans<<endl;
// 1 2 3 4
// 5 6 7 8
// 9 1 2 3
}
return 0;
}