[LeetCode] 1219. Path with Maximum Gold

In a gold mine grid of size m * 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:

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

黄金矿工。

你要开发一座金矿,地质勘测学家已经探明了这座金矿中的资源分布,并用大小为 m * n 的网格 grid 进行了标注。每个单元格中的整数就表示这一单元格中的黄金数量;如果该单元格是空的,那么就是 0。

为了使收益最大化,矿工需要按以下规则来开采黄金:

每当矿工进入一个单元,就会收集该单元格中的所有黄金。
矿工每次可以从当前位置向上下左右四个方向走。
每个单元格只能被开采(进入)一次。
不得开采(进入)黄金数目为 0 的单元格。
矿工可以从网格中 任意一个 有黄金的单元格出发或者是停止。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-with-maximum-gold
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是回溯backtracking。注意题目里面的几个条件。既然问的是最大受益,所以二维数组每个坐标你都需要做回溯,以判断到底从哪个坐标开始挖矿的收益最大。写回溯函数的时候,注意处理越界以及当前cell值为0的情况。遇到cell值为0,直接返回0,因为这个地方按照题意你是不能访问/开采的。对于一般情况,你需要看一下四个方向往下回溯哪一个方向的收益最大,返回那个最大的收益。同时既然是回溯,在回溯的最后记得把修改过的cell值再改回来。

时间O(k * 4 * 3 ^ (k - 1)) = O(4 * 3 ^ k) - 你最多有K个cell需要作为起点去访问,对于每个cell你最多有四种情况需要判断,但是对于下一个cell你只需要判断三种情况

空间O(k) - 回溯使用到的栈空间

Java实现

 1 class Solution {
 2     public int getMaximumGold(int[][] grid) {
 3         // corner case
 4         if (grid == null || grid.length == 0) {
 5             return 0;
 6         }
 7         // normal case
 8         int res = 0;
 9         for (int i = 0; i < grid.length; i++) {
10             for (int j = 0; j < grid[0].length; j++) {
11                 res = Math.max(res, dfs(grid, i, j));
12             }
13         }
14         return res;
15     }
16 
17     private int dfs(int[][] grid, int i, int j) {
18         // 越界处理和当cell值为0的时候不visit
19         if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
20             return 0;
21         }
22 
23         // grid[i][j]上的值
24         int origin = grid[i][j];
25         // mark as 0 after visited
26         grid[i][j] = 0;
27         int up = dfs(grid, i, j - 1);
28         int down = dfs(grid, i, j + 1);
29         int left = dfs(grid, i - 1, j);
30         int right = dfs(grid, i + 1, j);
31         int max = Math.max(left, Math.max(right, Math.max(up, down)));
32         // 还原
33         grid[i][j] = origin;
34         return grid[i][j] + max;
35     }
36 }

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/13900283.html