这条题目用的偏移量枚举,这种构造题多举几个例子其实挺简单的(一开始以为跟bfs dfs有关)
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
vector<vector<long long>> grid(n + 2, vector<long long>(m + 2, 0));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> grid[i][j];
}
}
vector<pair<int, int>> dirs = {
{-2, 0}, {-1, -1}, {-1, 0}, {-1, 1},
{0, -2}, {0, -1}, {0, 0}, {0, 1}, {0, 2},
{1, -1}, {1, 0}, {1, 1}, {2, 0}
};
vector<vector<long long>> val(n + 2, vector<long long>(m + 2, 0));
for (int x0 = 1; x0 <= n; ++x0) {
for (int y0 = 1; y0 <= m; ++y0) {
for (auto& d : dirs) {
int dx = d.first;
int dy = d.second;
int x = x0 + dx;
int y = y0 + dy;
if (x >= 1 && x <= n && y >= 1 && y <= m) {
val[x0][y0] += grid[x][y];
}
}
}
}
long long max_val = -1;
int best_x = 1, best_y = 1;
for (int x = 1; x <= n; ++x) {
for (int y = 1; y <= m; ++y) {
if (val[x][y] > max_val) {
max_val = val[x][y];
best_x = x;
best_y = y;
}
}
}
while (q--) {
int x, y, z;
cin >> x >> y >> z;
for (auto& d : dirs) {
int dx = d.first;
int dy = d.second;
int x0 = x + dx;
int y0 = y + dy;
if (x0 >= 1 && x0 <= n && y0 >= 1 && y0 <= m) {
val[x0][y0] += z;
if (val[x0][y0] > max_val) {
max_val = val[x0][y0];
best_x = x0;
best_y = y0;
}
}
}
cout << best_x << " " << best_y << "\n";
}
return 0;
}

京公网安备 11010502036488号