AcWing 2684. 矩阵中移动的最大次数
原题链接
中等
作者:
我是java同学
,
2023-09-19 21:35:05
,
所有人可见
,
阅读 80
class Solution {
public:
int dx[3] = {-1, 0, 1};
int dy[3] = {1, 1, 1};
vector<vector<int>> g;
int n, m;
int ans;
int maxMoves(vector<vector<int>>& grid) {
g = grid;
n = g.size(), m = g[0].size();
for (int i = 0; i < n; i ++ )
dfs(i, 0, 0);
return ans;
}
void dfs(int x, int y, int cnt) {
ans = max(ans, cnt);
if (ans == m - 1) return;
if (ans > cnt + m - y) return;
for (int i = 0; i < 3; i ++ ) {
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (g[x][y] < g[a][b])
dfs(a, b, cnt + 1);
}
}
};