leetcode 1219. Path with Maximum Gold 解法 python

一.问题描述

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.

二.解决思路

这种题目比较明显要用深度优先搜索来解决,尝试当前路径后记录当前路径下的值,然后回溯到上一层

具体来说:遍历grid,对每一个cell值不是0的进行dfs,记录值

具体的dfs的话请自行百度

我比较喜欢用递归解决所有东西,所以我实现的dfs不是那么标准

当然最好还是用标准的dfs,写算法题和真正应用不一样,标准dfs也可以帮我们理清思路

主要要注意的地方有两点:

第一:注意python的深拷贝和浅拷贝

第二:在某条路径下递归完后记得把isvisited给重新设置成false,不然会影响其他方向上的迭代

更新:自己还是写了个稍微标准点的,不知为啥最后TLE?

最后看了一下可能问题出在第二个版本我用了集合来追踪cell的状态,但是集合操作如add和remove很花时间?

还有其他想法的欢迎评论区交流或者私信!

建议直接看第二个版本的,第二个版本逻辑清楚一些

更多leetcode算法题解法请关注我的专栏leetcode算法从零到结束或关注我

欢迎大家一起套路一起刷题一起ac

三.源码

import copy
class Solution:
    def getMaximumGold(self, grid: List[List[int]]) -> int:
        maxnum=-1
        is_visited=[[False for j in range(len(grid[0]))] for i in range(len(grid))]
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j]!=0:
                    temp=copy.deepcopy(is_visited)
                    temp[i][j]=True
                    count=self.searchPath(i,j,grid,temp,grid[i][j])
                    maxnum=count if count>maxnum else maxnum
        return maxnum
    
    def searchPath(self, i,j,grid,is_visited,count):
        num1,num2,num3,num4=count,count,count,count
        if i-1>=0 and grid[i-1][j] and not is_visited[i-1][j]:
            is_visited[i-1][j]=True
            num1=self.searchPath(i-1,j,grid,is_visited,count+grid[i-1][j])
            is_visited[i-1][j]=False
        if i+1=0 and grid[i][j-1] and not is_visited[i][j-1]:
            is_visited[i][j-1]=True
            num3=self.searchPath(i,j-1,grid,is_visited,count+grid[i][j-1])
            is_visited[i][j-1]=False
        if j+1
class Solution:
    def getMaximumGold(self, grid: List[List[int]]) -> int:
        is_visited=set()
        return max(self.DFS(i,j,grid,is_visited) for j in range(len(grid[0])) \
                                            for i in range(len(grid)) if grid[i][j]!=0)
    
    def DFS(self, i,j,grid,is_visited):
        if i<0 or i>=len(grid) or j<0 or j>=len(grid[0]) or (i,j) in is_visited \
        or grid[i][j]==0:
            return -1
        is_visited.add((i,j))
        rst=grid[i][j]+max(0,max(self.DFS(i+x,j+y,grid,is_visited) \
                                    for x,y in [[-1,0],[1,0],[0,-1],[0,1]]))
        is_visited.remove((i,j))
        return rst
        

 

你可能感兴趣的:(leetcode算法从零到结束)