【leetcode】1219. Path with Maximum Gold
题目如下:
In a gold mine
grid
of sizem * 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.
解题思路:DFS或者BFS都可以。本题主要是需要记录遍历过的节点,防止重复遍历陷入死循环。我的记录方法是利用整数的位操作,给grid中每个节点都分配一个序号,按从左往右从上往下的顺序,(0,0)是2^0,(0,1)是2^1次方,依次类推。
代码如下:
class Solution(object):
def getMaximumGold(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
def getNumber(x,y):
v = x*len(grid[0]) + y
return 2**v
res = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 0:continue
count = grid[i][j]
flag = 0
queue = [(i,j,count,flag | getNumber(i,j))]
direction = [(0,1),(0,-1),(1,0),(-1,0)]
while len(queue) > 0:
x,y,count,flag = queue.pop(0)
res = max(res,count)
for (x1,y1) in direction:
if x1 + x >= 0 and x1+x < len(grid) and y+y1 >=0 and y+y1 < len(grid[0]) and grid[x+x1][y+y1] != 0 \
and flag & getNumber(x1+x,y1+y) == 0:
new_count = count + grid[x1+x][y1+y]
queue.append((x+x1,y+y1,new_count,flag | getNumber(x1+x,y1+y)))
return res
【leetcode】1219. Path with Maximum Gold的更多相关文章
- 【LeetCode】1102. Path With Maximum Minimum Value 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+并查集 优先级队列 日期 题目地址:https: ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- LeetCode 1219. Path with Maximum Gold
原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/ 题目: In a gold mine grid of size m * n, ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【LeetCode】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- 【leetcode】Simplify Path
题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
随机推荐
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- Maven - Maven3实战学习笔记(2)坐标和依赖
1.maven坐标元素 maven坐标元素包括:groupId.artifactId.version.packaging.classifier. classifier:定义输出的附属构件.groupI ...
- [题解][SHOI2013]超级跳马 动态规划/递推式/矩阵快速幂优化
这道题... 让我见识了纪中的强大 这道题是来纪中第二天(7.2)做的,这么晚写题解是因为 我去学矩阵乘法啦啦啦啦啦对矩阵乘法一窍不通的童鞋戳链接啦 层层递推会TLE,正解矩阵快速幂 首先题意就是给你 ...
- Makefile中include、-include、sinclude
include.-include.sinclude使用 在 Makefile 使用 include 关键字可以把别的 Makefile 包含进来,这很像 C 语言的#include,被包含的文件会原模 ...
- Python常用库整理
Python常用库整理 Python中到底有哪些库会让程序员爱不释手?以至于一次上瘾,造成永久性伤害(这句话好像在哪里见过),今天我们就来整理一番这样的库,欢迎各位在评论区或者私信我添加或者修改相关库 ...
- E - 卿学姐与城堡的墙(树状数组求逆序数)
卿学姐与城堡的墙 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- 链接Caffe,程序报错应用程序无法正常启动(0xc000007b)
目录 背景 Debug 解决办法 原因(猜想) 总结 重点是介绍了一种排查这个问题的方法. 背景 Windows 下, Caffe 单独编译成库并且安装在路径 Caffe_DIR, 动态链接库 Caf ...
- synchronize和lock的区别 & synchionzie与volatile的区别
synchronized与Lock的区别 https://www.cnblogs.com/iyyy/p/7993788.html Lock和synchronized和volatile的区别和使用 ht ...
- Homebrew学习(一)之初认识
Homebrew Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,会 ...
- lamp项目上线流程简述 (ubuntu16.04 )
1 新建一个sudo用户,而不是直接用root操作 ① 新建用户可参考 https://www.cnblogs.com/bushuwei/p/10880182.html ② 赋予sudo权限: ...