Path with Maximum Gold - Problem
Path with Maximum Gold
Imagine you're a treasure hunter exploring an ancient gold mine! You have a rectangular grid representing the mine, where each cell contains a certain amount of gold (or 0 if it's empty rock).
Your mission is to find the path that allows you to collect the maximum amount of gold possible. Here are the rules of your expedition:
๐ Movement: From any cell, you can move one step in any of the 4 directions (up, down, left, right)
๐ฐ Collection: You automatically collect all gold in each cell you visit
๐ซ No Revisiting: Once you've been to a cell, you can't return to it in the same path
โ No Empty Cells: You cannot step on cells with 0 gold
๐ฏ Flexible Start/End: You can begin and end your journey at any cell containing gold
Return the maximum amount of gold you can collect in a single expedition!
Example: In a 3ร3 grid with gold values
Imagine you're a treasure hunter exploring an ancient gold mine! You have a rectangular grid representing the mine, where each cell contains a certain amount of gold (or 0 if it's empty rock).
Your mission is to find the path that allows you to collect the maximum amount of gold possible. Here are the rules of your expedition:
๐ Movement: From any cell, you can move one step in any of the 4 directions (up, down, left, right)
๐ฐ Collection: You automatically collect all gold in each cell you visit
๐ซ No Revisiting: Once you've been to a cell, you can't return to it in the same path
โ No Empty Cells: You cannot step on cells with 0 gold
๐ฏ Flexible Start/End: You can begin and end your journey at any cell containing gold
Return the maximum amount of gold you can collect in a single expedition!
Example: In a 3ร3 grid with gold values
[[1,2,3],[0,2,0],[7,6,5]], you could start at cell (2,0) with value 7, then move to collect 6, then 2, then 3 for a total of 18 gold! Input & Output
example_1.py โ Basic Grid
$
Input:
grid = [[1,2,3],[0,2,0],[7,6,5]]
โบ
Output:
18
๐ก Note:
The optimal path starts at (2,0) with value 7, moves to (2,1) with value 6, then to (1,1) with value 2, and finally to (0,2) with value 3. Total: 7 + 6 + 2 + 3 = 18
example_2.py โ Single Row
$
Input:
grid = [[1,2,3,4]]
โบ
Output:
10
๐ก Note:
We can traverse the entire row from left to right (or right to left) collecting all gold: 1 + 2 + 3 + 4 = 10
example_3.py โ All Zeros
$
Input:
grid = [[0,0],[0,0]]
โบ
Output:
0
๐ก Note:
No cells contain gold, so we cannot collect anything. The maximum gold is 0.
Constraints
- 1 โค m, n โค 15 (grid dimensions)
- 0 โค grid[i][j] โค 100
- Grid will have at least one cell with gold > 0
Visualization
Tap to expand
Understanding the Visualization
1
Survey the Mine
Look at all possible starting positions with gold
2
Pick Starting Point
Begin DFS from a cell with gold, mark it as visited
3
Explore Neighbors
Check all 4 directions for valid moves (gold > 0, not visited)
4
Go Deeper
Recursively explore the best neighboring cell
5
Hit Dead End
When no more moves possible, record the gold collected
6
Backtrack
Unmark current cell and try alternative paths
7
Find Maximum
Compare all path totals and return the maximum
Key Takeaway
๐ฏ Key Insight: This problem requires exploring all possible paths systematically. Backtracking allows us to 'undo' our moves and try alternatives, ensuring we find the path with maximum gold while respecting the constraint of not revisiting cells.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code