【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 ...
随机推荐
- Tlbimp 生成c#互操作com组件dll;Tlbimp 生成.net互操作com组件dll
Tlbimp 生成c#互操作com组件dll vs开发人员命令提示>进入到com组件目录>Tlbimp com.dll
- 根据输入的整数n使得输出精确到小数n位
#include<iostream> #include<stdio.h> using namespace std; int main(){ int a,b,c; while(t ...
- docker 运行mysql最新版本用navicat连接报错:1251
主要是:新版的mysql的加密方式发生了变化,解决方法如下: 1:进入docker容器: docker exec -it mysql(启动mysql时候起的别名) /bin/bash 2:登陆my ...
- Spring IoC容器管理Action
Spring IoC容器管理Action有两种方式:DelegatingRequestProcessor.DelegatingActionProxy 不管采用哪一种方式,都需要随应用启动时创建Appl ...
- selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH
问题,找不到’geckodriver’ 的环境path,解决方案 下载geckodriver.exe 放到Firefox的安装目录下,如:(D:\火狐\Mozilla Firefox): 将火狐安装目 ...
- java方法形参是引用类型
public void 方法名(Student s) 这里形参需要的是该类的对象或者子类对象(父类引用子类对象). 1.若为普通类:则可传入该类的实例对象即可,方法名(new Student()): ...
- Java四类八项基本数据类型
一. 四类八项基本数据类型 1. 整数类型(byte.short.int.long) 三点注意事项: a. Java各整数类型有固定的表示范围和字段长度,其不收操作系统的影响,以保持Java的可移植性 ...
- PythonDay12
day12内置_函数 今日内容 生成器 推导式 内置函数一 生成器 什么是生成器?生成器的本质就是一个迭代器 迭代器是python自带的 生成器是程序员自己写的一种迭代器 生成器编写方式: 1.基于函 ...
- 用Kindle阅读PDF最简单的3个方法!
老实说,Kindle 对于PDF文件是很不友好的,经常会出现各种排版问题,所以,对电子阅读器方面比较了解的同学都知道,如果需要经常用阅读器查看PDF文件的话,最好还是买一款更大屏幕的设备,而Kindl ...
- 简单CSS实现闪烁动画(+1白话讲解)
原文:简单CSS实现闪烁动画(+1白话讲解) 本文转载于:猿2048网站⇒https://www.mk2048.com/blog/blog.php?id=icj2chj2ab 背景 本文承接自上文&l ...