public: intgetMaximumGold(vector<vector<int>>& grid){ int m = grid.size(), n = grid[0].size(); int ans = 0;
function<void(int, int, int)> dfs = [&](int x, int y, int gold) { gold += grid[x][y]; ans = max(ans, gold);
int rec = grid[x][y]; grid[x][y] = 0;
for (int d = 0; d < 4; ++d) { int nx = x + dirs[d][0]; int ny = y + dirs[d][1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] > 0) { dfs(nx, ny, gold); } }
grid[x][y] = rec; };
for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] != 0) { dfs(i, j, 0); } } }
publicintgetMaximumGold(int[][] grid) { this.grid = grid; this.m = grid.length; this.n = grid[0].length; for (inti=0; i < m; ++i) { for (intj=0; j < n; ++j) { if (grid[i][j] != 0) { dfs(i, j, 0); } } } return ans; }
publicvoiddfs(int x, int y, int gold) { gold += grid[x][y]; ans = Math.max(ans, gold);
intrec= grid[x][y]; grid[x][y] = 0;
for (intd=0; d < 4; ++d) { intnx= x + dirs[d][0]; intny= y + dirs[d][1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] > 0) { dfs(nx, ny, gold); } }
publicclassSolution { staticint[][] dirs = {newint[]{-1, 0}, newint[]{1, 0}, newint[]{0, -1}, newint[]{0, 1} }; int[][] grid; int m, n; int ans = 0;
publicintGetMaximumGold(int[][] grid) { this.grid = grid; this.m = grid.Length; this.n = grid[0].Length; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] != 0) { DFS(i, j, 0); } } } return ans; }
publicvoidDFS(int x, int y, int gold) { gold += grid[x][y]; ans = Math.Max(ans, gold);
int rec = grid[x][y]; grid[x][y] = 0;
for (int d = 0; d < 4; ++d) { int nx = x + dirs[d][0]; int ny = y + dirs[d][1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] > 0) { DFS(nx, ny, gold); } }
for (let d = 0; d < 4; ++d) { const nx = x + this.dirs[d][0]; const ny = y + this.dirs[d][1]; if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] > 0) { dfs(nx, ny, gold); } }
grid[x][y] = rec; } for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (grid[i][j] !== 0) { dfs(i, j, 0); } } } return ans; };