diff --git a/leetcode/1201-1300/1219.Path-with-Maximum-Gold/README.md b/leetcode/1201-1300/1219.Path-with-Maximum-Gold/README.md index 1ad834ac8..4464c1512 100644 --- a/leetcode/1201-1300/1219.Path-with-Maximum-Gold/README.md +++ b/leetcode/1201-1300/1219.Path-with-Maximum-Gold/README.md @@ -1,28 +1,41 @@ # [1219.Path with Maximum Gold][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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: a = "11", b = "1" -Output: "100" +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:** -## 题解 - -### 思路1 -> ... -Path with Maximum Gold -```go ``` - +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. +``` ## 结语 diff --git a/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution.go b/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution.go index d115ccf5e..34f2aa281 100644 --- a/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution.go +++ b/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution.go @@ -1,5 +1,38 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(grid [][]int) int { + rows, cols := len(grid), len(grid[0]) + + var dfs func(int, int, [][]int) int + dfs = func(x, y int, matrix [][]int) int { + if x < 0 || x >= rows || y < 0 || y >= cols || matrix[x][y] == 0 { + return 0 + } + cur := matrix[x][y] + matrix[x][y] = 0 + a := dfs(x-1, y, matrix) + a = max(a, dfs(x+1, y, matrix)) + a = max(a, dfs(x, y-1, matrix)) + a = max(a, dfs(x, y+1, matrix)) + matrix[x][y] = cur + + return cur + a + } + matrix := make([][]int, rows) + for i := 0; i < rows; i++ { + matrix[i] = make([]int, cols) + } + ans := 0 + for i := 0; i < rows; i++ { + for j := 0; j < cols; j++ { + + for k := 0; k < rows; k++ { + copy(matrix[k], grid[k]) + } + if grid[i][j] != 0 { + ans = max(ans, dfs(i, j, matrix)) + } + } + } + return ans } diff --git a/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution_test.go b/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution_test.go index 14ff50eb4..836799265 100644 --- a/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution_test.go +++ b/leetcode/1201-1300/1219.Path-with-Maximum-Gold/Solution_test.go @@ -10,12 +10,18 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs [][]int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", [][]int{ + {0, 6, 0}, {5, 8, 7}, {0, 9, 0}, + }, 24}, + {"TestCase2", [][]int{ + {1, 0, 7}, {2, 0, 6}, {3, 4, 5}, {0, 3, 0}, {9, 0, 20}, + }, 28}, + {"TestCase3", [][]int{ + {1, 0, 7, 0, 0, 0}, {2, 0, 6, 0, 1, 0}, {3, 5, 6, 7, 4, 2}, {4, 3, 1, 0, 2, 0}, {3, 0, 5, 0, 20, 0}, + }, 60}, } // 开始测试 @@ -30,10 +36,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }