Sponsored
Sponsored
This approach utilizes backtracking and depth-first search (DFS) to explore all possible paths to collect the maximum gold. Start from each cell containing gold and perform DFS to explore in all four possible directions (up, down, left, right). During the search, keep track of the total gold collected. Use backtracking to unvisit a cell after exploring all potential paths from it.
Time complexity: O((m*n)^2) - In the worst-case scenario, each cell containing gold gets visited and backtracked.
Space complexity: O(m*n) - This results from recursive stack space and the grid modifications.
1class Solution {
2 private int dfs(int[][] grid, int x, int y) {
3 if (x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] == 0) return 0;
4
5 int originalGold = grid[x][y];
6 grid[x][y] = 0; // Mark as visited
7 int maxGold = 0;
8 int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
9 for (int[] dir : dirs) {
10 maxGold = Math.max(maxGold, dfs(grid, x + dir[0], y + dir[1]));
11 }
12 grid[x][y] = originalGold; // Backtrack
13 return originalGold + maxGold;
14 }
15
16 public int getMaximumGold(int[][] grid) {
17 int maxGold = 0;
18 for (int i = 0; i < grid.length; i++) {
19 for (int j = 0; j < grid[0].length; j++) {
20 if (grid[i][j] > 0) {
21 maxGold = Math.max(maxGold, dfs(grid, i, j));
22 }
23 }
24 }
25 return maxGold;
26 }
27}
This Java implementation maintains a DFS traversal strategy using grid state alterations for path exploration. It employs a movement matrix to handle traversal directionally in a clean, concise way. Backtracking enables full exploration from each gold-laden starting cell, collecting maximum gold possible.
This approach involves using dynamic programming with bit masking to track visited states and optimize the path of maximum gold collection. It systematically calculates the maximum gold that can be gathered by starting from each gold cell and considering valid moves while ensuring no cell is revisited.
Time complexity: O(2^(m*n) * m * n) given bitmask state examination.
Space complexity: O(2^(m*n) * m * n) due to DP storage needs for various path states.
1#include <algorithm>
#include <cstring>
using namespace std;
class Solution {
int dp[1 << 15][15][15];
int dirs[5] = {0, 1, 0, -1, 0};
public:
int solve(vector<vector<int>>& grid, int mask, int x, int y) {
if (dp[mask][x][y] != -1) return dp[mask][x][y];
int maxGold = 0;
for (int d = 0; d < 4; ++d) {
int nx = x + dirs[d], ny = y + dirs[d + 1];
if (nx >= 0 && ny >= 0 && nx < grid.size() && ny < grid[0].size() && grid[nx][ny] > 0
&& !(mask & (1 << (nx * grid[0].size() + ny)))) {
int newMask = mask | (1 << (nx * grid[0].size() + ny));
maxGold = max(maxGold, solve(grid, newMask, nx, ny));
}
}
return dp[mask][x][y] = grid[x][y] + maxGold;
}
int getMaximumGold(vector<vector<int>>& grid) {
memset(dp, -1, sizeof(dp));
int maxGold = 0, m = grid.size(), n = grid[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] > 0) {
int mask = 1 << (i * n + j);
maxGold = max(maxGold, solve(grid, mask, i, j));
}
}
}
return maxGold;
}
};
The C++ strategy employs dynamic programming and bit-masking to address cell visted states, similar to the C-underpinning logic. This coverage aims to derive optimal routes leveraging memory efficiently through DP arrays. Referential paths are stored as bitmask iterations with recursive resolution aiding rapid path enumeration.