leetcode-157周赛-5215黄金矿工
题目描述:


方法一:dfs
class Solution:
def getMaximumGold(self, grid: List[List[int]]) -> int:
maxx = 0
R, C = len(grid), len(grid[0]) def dfs(r, c, visited, count):
nonlocal maxx
count += grid[r][c]
maxx = max(maxx, count)
visited.add((r, c))
for nr, nc in [[r+1, c], [r, c+1], [r-1, c], [r, c-1]]:
if 0 <= nr < R and 0 <= nc < C and grid[nr][nc] != 0 and (nr, nc) not in visited:
dfs(nr, nc, visited, count)
visited.remove((r, c))
count -= grid[r][c] for r in range(len(grid)):
for c in range(len(grid[0])):
if grid[r][c] != 0:
dfs(r, c, set(), 0) return maxx
leetcode-157周赛-5215黄金矿工的更多相关文章
- LeetCode 5215. 黄金矿工(Java)DFS
题目: 5215. 黄金矿工 你要开发一座金矿,地质勘测学家已经探明了这座金矿中的资源分布,并用大小为 m * n 的网格 grid 进行了标注.每个单元格中的整数就表示这一单元格中的黄金数量:如果该 ...
- [Tyvj Aug11] 黄金矿工
传送门 Description 黄金矿工是一个经典的小游戏,它可以锻炼人的反应能力.该游戏中,可以通过“挖矿”获得积分并不断升级.玩家可以在线玩flash版黄金矿工,也可以下载后玩单机版黄金矿工.目前 ...
- 洛谷3961 [TJOI2013]黄金矿工
题目描述 小A最近迷上了在上课时玩<黄金矿工>这款游戏.为了避免被老师发现,他必须小心翼翼,因此他总是输.在输掉自己所有的金币后,他向你求助.每个黄金可以看做一个点(没有体积).现在给出你 ...
- 黄金矿工(LeetCode Medium难度)1129题 题解(DFS)
题目描述: 给定一个二维网络,给定任意起点与终点.每一步可以往4个方向走.要找出黄金最多的一条线路. 很明显的是要“一条路走到黑,一直下去直到某个条件停止”. 运用dfs(深度优先搜索)求解. 因为起 ...
- [loj574]黄金矿工
记$dep_{x}$为1到$x$的边权和,当$x$上的矿工挖了$y$上的黄金时($y$在$x$子树内),显然$\sum_{e}c_{e}=dep_{y}-dep_{x}$ 由此,对于$u$上权值为$v ...
- [LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...
- ✡ leetcode 157. Read N Characters Given Read4 利用read4实现read --------- java
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- [LeetCode#157] Read N Characters Given Read4
Problem: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is ...
- 洛咕 P3961 [TJOI2013]黄金矿工
甚至都不是树形背包= = 把每条线抠出来,这一条线就是个链的依赖关系,随便背包一下 // luogu-judger-enable-o2 #include<bits/stdc++.h> #d ...
随机推荐
- ASP.net简单分页
//控制器 //实例化实体 TestEntities1 test =new TestEntities1(); //定义页数 int pageIndex; //查看接收的页数 如果不能转 则重置为1 i ...
- mybatis的实际应用
简单基本的增删改查语句就不说了,直接从一对一,一对多的关系开始: association联合:联合元素用来处理“一对一”的关系; collection聚集:聚集元素用来处理“一对多”的关系; MyBa ...
- Samcompu Loves Water
题目背景 Samcompu拥有大量的"水"资源!! 题目描述 Samcompu需要制定一个水计划.这个计划的主要目的就是为了避开老师监视的时间来水. 老师在中途会离开机房T次,第i ...
- C语言结构体注意点
#include <stdio.h> int main() { /*************************************************** *定义结构体变量的 ...
- 使用tensorboard报错 ImportError: No module named past.builtins
安装 future pip install future conda install future
- 基于物品的协同过滤(ItemCF)
- 【学术篇】2.28测试T2 线段 拓扑排序
题目: 思路: 看到这种找前后的题目... 第一反应就是拓扑排序_(:з」∠)_ 每条线段都有左右两个端点咯, 然后就乱搞吧.. 我们用\(i\)和\(i'\)分别表示第\(i\)条线段的左右端点.. ...
- linux下设置Git
目录 ## Git介绍 1.工作原理 2.SVN与Git的最主要的区别? 3.操作 4.创建本地仓库 5.把文件添加到本地仓库 6.版本回退 7.理解工作区(workspace)与暂存区(index) ...
- java----int,string 转化为long
String: 1.返回Long包装类型: String str = "aaa"; long l = Long.parseLong([str]); 2.返回long基本数据类型: ...
- QueryList getData()方法中多次调用来实现递归多级采集。
<?php require 'QueryList/vendor/autoload.php'; use QL\QueryList; //获取每个li里面的h3标签内容,和class为item的元素 ...