Pagini recente » Cod sursa (job #2311638) | Cod sursa (job #91204) | Cod sursa (job #3237635) | Cod sursa (job #471965) | Cod sursa (job #3134202)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
ifstream f("plantatie.in");
ofstream g("plantatie.out");
int N, M;
f >> N >> M;
vector<vector<int>> plantatie(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
f >> plantatie[i][j];
}
}
vector<vector<int>> dp(N + 1, vector<int>(N + 1, 0));
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + plantatie[i - 1][j - 1];
}
}
while (M--) {
int i, j, k;
f >> i >> j >> k;
int maxProductivitate = 0;
for (int x = i; x < i + k; x++) {
for (int y = j; y < j + k; y++) {
int suma = dp[x][y] - dp[x - k][y] - dp[x][y - k] + dp[x - k][y - k];
maxProductivitate = max(maxProductivitate, suma);
}
}
g << maxProductivitate << "\n";
}
f.close();
g.close();
return 0;
}