1. 题目描述
2. 思路分析
整体思路:二维前缀和 (模板在这https://blog.csdn.net/m0_62531913/article/details/132712831?spm=1001.2014.3001.5502)
先对数组进行二维前缀和,定义一个变量maxx表示首都土地价值和最大值,变量x记录此时的横坐标,变量y记录此时的纵坐标。由题意知,首都是一个占地C*C的正方形,我们以此来枚举这个这个地图上符合条件的每个子矩阵左上角的坐标,通过遍历求得最大的首都土地价值和。
3. 代码实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1010;
ll a[N][N], s[N][N];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll n, m, c; cin >> n >> m >> c;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
s[i][j] = a[i][j] + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];
}
}
ll maxx = 0, x = 0, y = 0;
for (int i = 1; i <= n - c + 1; i++) {
for (int j = 1; j <= m - c + 1; j++) {
if (maxx < s[i + c - 1][j + c - 1] - s[i + c - 1][j - 1] - s[i - 1][j + c - 1] + s[i - 1][j - 1]) {
maxx = s[i + c - 1][j + c - 1] - s[i + c - 1][j - 1] - s[i - 1][j + c - 1] + s[i - 1][j - 1];
x = i;
y = j;
}
}
}
cout << x << " " << y << '\n';
return 0;
}