Skip to content

Latest commit

 

History

History
203 lines (168 loc) · 4.91 KB

File metadata and controls

203 lines (168 loc) · 4.91 KB

中文文档

Description

In a gold mine grid of size m x n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.

Return the maximum amount of gold you can collect under the conditions:

  • Every time you are located in a cell you will collect all the gold in that cell.
  • From your position, you can walk one step to the left, right, up, or down.
  • You can't visit the same cell more than once.
  • Never visit a cell with 0 gold.
  • You can start and stop collecting gold from any position in the grid that has some gold.

 

Example 1:

Input: grid = [[0,6,0],[5,8,7],[0,9,0]]
Output: 24
Explanation:
[[0,6,0],
 [5,8,7],
 [0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.

Example 2:

Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
Output: 28
Explanation:
[[1,0,7],
 [2,0,6],
 [3,4,5],
 [0,3,0],
 [9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 15
  • 0 <= grid[i][j] <= 100
  • There are at most 25 cells containing gold.

Solutions

DFS.

Python3

class Solution:
    def getMaximumGold(self, grid: List[List[int]]) -> int:
        def dfs(i, j):
            if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0 or vis[i][j]:
                return 0

            vis[i][j] = True
            t = 0
            for x, y in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
                t = max(t, dfs(i + x, j + y))
            vis[i][j] = False
            return t + grid[i][j]

        m, n = len(grid), len(grid[0])
        ans = 0
        vis = [[False] * n for _ in range(m)]
        for i in range(m):
            for j in range(n):
                ans = max(ans, dfs(i, j))
        return ans

Java

class Solution {
    private int[][] grid;
    private boolean[][] vis;

    public int getMaximumGold(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        this.grid = grid;
        this.vis = new boolean[m][n];
        int ans = 0;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                ans = Math.max(ans, dfs(i, j));
            }
        }
        return ans;
    }

    private int dfs(int i, int j) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0 || vis[i][j]) {
            return 0;
        }
        vis[i][j] = true;
        int t = 0;
        int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
        for (int[] dir : dirs) {
            t = Math.max(t, dfs(i + dir[0], j + dir[1]));
        }
        vis[i][j] = false;
        return t + grid[i][j];
    }
}

C++

class Solution {
public:
    vector<vector<int>> grid;
    vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    int getMaximumGold(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        this->grid = grid;
        vector<vector<bool>> vis(m, vector<bool>(n, false));
        int ans = 0;
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                ans = max(ans, dfs(i, j, vis));
        return ans;
    }

    int dfs(int i, int j,  vector<vector<bool>>& vis) {
        if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size() || grid[i][j] == 0 || vis[i][j]) return 0;
        vis[i][j] = true;
        int t = 0;
        for (auto& dir : dirs)
            t = max(t, dfs(i + dir[0], j + dir[1], vis));
        vis[i][j] = false;
        return t + grid[i][j];
    }
};

Go

func getMaximumGold(grid [][]int) int {
	m, n := len(grid), len(grid[0])
	vis := make([][]bool, m)
	for i := range vis {
		vis[i] = make([]bool, n)
	}

	var dfs func(i, j int) int
	dfs = func(i, j int) int {
		if i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0 || vis[i][j] {
			return 0
		}
		vis[i][j] = true
		dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
		t := 0
		for _, dir := range dirs {
			t = max(t, dfs(i+dir[0], j+dir[1]))
		}
		vis[i][j] = false
		return t + grid[i][j]
	}

	ans := 0
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			ans = max(ans, dfs(i, j))
		}
	}
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

...