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 ...
随机推荐
- layui中下拉框问题
<div class="layui-form-item"> <label class="layui-form-label">上级栏目:& ...
- erlang在windows下和虚拟机节点通信
版权声明:博客将逐步迁移到 http://cwqqq.com https://blog.csdn.net/cwqcwk1/article/details/24738599 在Linux下部署erlan ...
- Oracle中NEXTVAL 和 CURRVAL的使用
能够通过在 SQL 语句中使用 NEXTVAL 或 CURRVAL 运算符来訪问序列的值.必须用以 sequence.NEXTVAL 或sequence.CURRVAL 格式驻留在同一个数据库中的序列 ...
- Spring与Struts2 的整合使用
Spring与Struts2 的整合使用 项目结构 再Struts2 中(还没有与Spring整合时),它创建Action类的依据 <action name="second" ...
- zmq作为守护进程?等待连接
服务端是作为守护进程在运行的,客户端connect成功,但write时直接退出了,我在想肯能服务端socket在write时已经失效了,不然为什么会出现write时进程退出呢?现在的问题是,我要怎么才 ...
- day03 mysql外键 表的三种关系 单表查询 navicat
day03 mysql navicat 一.完整性约束之 外键 foreign key 一个表(关联表: 是从表)设置了外键字段的值, 对应的是另一个表的一条记录(被关联表: 是主 ...
- shell脚本中关于日期的操作
一.计算指定日期的前一天的日期 date -d "yesterday 20150401 " +%Y%m%d 二.如果获取当前日期的前一天 date -d " ...
- 【JZOJ3918】蛋糕
description 今天是Bessie的生日,他买了一个蛋糕和朋友们一起分享,蛋糕可以看成是一个R行C列的表格,共有R*C个格子,每个格子都有一个0至9的数字,表示该格子蛋糕拥有的巧克力.现在Be ...
- NX二次开发-UF_MODL_ask_point_containment获取一个点是在体(面,边)的边界内部,外部,还是边界上
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_curve.h> #include < ...
- NX二次开发-获得制图中对象的坐标点UF_DRF_ask_origin
#include <uf.h> #include <uf_ui.h> #include <uf_drf.h> #include <uf_obj.h> # ...