很简单的题目,利用二维前缀和即可。草纸上随手一画很直观。
核心代码:
mp[i][j] += mp[i - 1][j] + mp[i][j - 1] - mp[i - 1][j - 1];
// cout << mp[i][j] << " ";
int lx = 1, ly = 1;
lx = max (lx, i - m + 1);
ly = max (ly, j - m + 1);
max_ans = max (max_ans, mp[i][j] - mp[i][ly - 1] - mp[lx - 1][j] + mp[lx - 1][ly - 1]);
注意x和y可以是0,建议坐标读入+1处理。