class Solution:
def getMaximumGold(self, grid):
rows, cols = len(grid), len(grid[0])
res = 0
def dfs(x, y, total):
nonlocal res
gold = grid[x][y]
res = max(res, total + gold)
grid[x][y] = 0 # Mark as visited
for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)]:
nx, ny = x + dx, y + dy
if 0 <= nx < rows and 0 <= ny < cols and grid[nx][ny] > 0:
dfs(nx, ny, total + gold)
grid[x][y] = gold # Backtrack
for i in range(rows):
for j in range(cols):
if grid[i][j] > 0:
dfs(i, j, 0)
return res
class Solution {
public:
int getMaximumGold(vector<vector<int>>& grid) {
int res = 0, rows = grid.size(), cols = grid[0].size();
function<void(int,int,int)> dfs = [&](int x, int y, int total) {
int gold = grid[x][y];
res = max(res, total + gold);
grid[x][y] = 0;
vector<pair<int,int>> dirs = {{-1,0},{1,0},{0,-1},{0,1}};
for (auto [dx,dy]: dirs) {
int nx = x + dx, ny = y + dy;
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] > 0)
dfs(nx, ny, total + gold);
}
grid[x][y] = gold;
};
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
if (grid[i][j] > 0)
dfs(i, j, 0);
return res;
}
};
class Solution {
int res = 0, rows, cols;
public int getMaximumGold(int[][] grid) {
rows = grid.length; cols = grid[0].length;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (grid[i][j] > 0)
dfs(grid, i, j, 0);
return res;
}
void dfs(int[][] grid, int x, int y, int total) {
int gold = grid[x][y];
res = Math.max(res, total + gold);
grid[x][y] = 0;
int[] dx = {-1,1,0,0}, dy = {0,0,-1,1};
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] > 0)
dfs(grid, nx, ny, total + gold);
}
grid[x][y] = gold;
}
}
var getMaximumGold = function(grid) {
let res = 0, rows = grid.length, cols = grid[0].length;
function dfs(x, y, total) {
let gold = grid[x][y];
res = Math.max(res, total + gold);
grid[x][y] = 0;
for (let [dx, dy] of [[-1,0],[1,0],[0,-1],[0,1]]) {
let nx = x + dx, ny = y + dy;
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] > 0)
dfs(nx, ny, total + gold);
}
grid[x][y] = gold;
}
for (let i = 0; i < rows; i++)
for (let j = 0; j < cols; j++)
if (grid[i][j] > 0)
dfs(i, j, 0);
return res;
};
You are given a 2D grid representing a gold mine. Each cell in the grid contains an integer indicating the amount of gold present, or zero if there is none.
Your goal is to collect as much gold as possible by moving from one cell to another. The rules are:
grid
.
When approaching this problem, the first idea might be to try every possible path starting from every gold cell and keep track of the maximum gold collected. This brute-force approach is feasible because the grid is small (at most 25x25), but we want to make it as efficient as possible.
The challenge is to avoid revisiting cells and to ensure we only move to adjacent gold-containing cells. Since we can start from any gold cell, we must consider all possible starting points.
The problem is similar to finding the "maximum path sum" in a graph where each node is a gold cell and edges connect adjacent gold cells. A depth-first search (DFS) is a natural fit for exploring all possible paths, as it allows us to mark cells as visited and backtrack efficiently.
We also realize that since we can start from any gold cell, we need to try a DFS from each one. The key optimization is to use in-place marking (temporarily setting the cell to 0) to avoid extra memory usage for visited flags, and to backtrack (restore the value) after each path.
Let's break down the solution step by step:
Let's consider the following grid:
[ [0, 6, 0], [5, 8, 7], [0, 9, 0] ]Step-by-step:
Brute-force:
This problem is a classic example of using depth-first search with backtracking to explore all possible paths in a grid while avoiding revisiting cells. By starting DFS from every gold cell, marking cells as visited in-place, and backtracking, we ensure every potential path is checked. The approach is efficient due to the small grid size, and the solution elegantly leverages recursion and careful state management to find the maximum gold path.