在吴学长的代码上做了一点简化修改,本质一样。在外面铺了一圈0,让代码更简单一点,不用考虑边界情况了。
题目: http://acm.sjtu.edu.cn/OnlineJudge/problem/1002
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { int L,W; cin>>L>>W; int** sums = new int*[L+1]; for (int i = 0; i <= L; ++i) sums[i] = new int[W+1]; //memset(sums,0,sizeof(sums)); //input and store the sum for (int i = 1; i <= L; ++i) { for (int j = 1; j <= W; ++j) { int t; cin>>t; //sum[i][j] can be considered as three components:t,up,left-sidebar sums[i][j] = t + sums[i-1][j] +
(sums[i][j-1] - sums[i-1][j-1]); } } //move the scope and calculate the maximum int a,b; cin>>a>>b; int max=0; for (int i = a; i <= L; ++i) { for (int j = b; j <= W; ++j) { //sum - left - up int tmp = 0; tmp = sums[i][j] - sums[i][j-b]
- (sums[i-a][j] - sums[i-a][j-b]); if(tmp > max) max = tmp; } } cout<<max<<endl; for (int i = 0; i < L+1; ++i) { delete[] sums[i]; } delete[] sums; return 0; }
【算法学习笔记】24.记忆化搜索 解题报告 SJTU_OJ 1002 二哥种花生
原文:http://www.cnblogs.com/yuchenlin/p/4381691.html