CF1676D X-Sum 题解
题意
给出一个有权值的棋盘, 现在要求你选定一个象的位置, 使象可以攻击的点的权值和最大。
象的攻击方法是斜着走。
解
看到数据范围这么小, 可以想到用暴力, 每个点都计算可能到达的地方的权值和。
至于计算权值和,四个方向模拟一下就可以了。
#include <bits/stdc++.h> using namespace std; using i64 = long long; constexpr int inf = INT_MAX; constexpr i64 lnf = LONG_LONG_MAX; constexpr int N = 210; constexpr int dx[] = {1, -1}, dy[] = {1, -1}; int a[N][N]; void solve() { memset(a, 0, sizeof a); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } function<int(int, int)> dfs = [&](int x, int y) { int X = x, Y = y, ans = 0; while (X >= 0 && Y >= 0) { ans += a[X][Y]; X--, Y--; } X = x, Y = y; while (X >= 0 && Y < m) { X--, Y++; if (X >= 0 && Y < m) ans += a[X][Y]; } X = x, Y = y; while (X < n && Y >= 0) { X++, Y--; if (X < n && Y >= 0) ans += a[X][Y]; } X = x, Y = y; while (X < n && Y < m) { X++, Y++; if (X < n && Y < m) ans += a[X][Y]; } return ans; }; int ans = -inf; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans = max(ans, dfs(i, j)); } } cout << ans << endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { solve(); } return 0; }
本文作者:Falling-flowers 的 blog
本文链接:https://www.cnblogs.com/falling-flowers/p/16296456.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步