LeetCode 5215. 黄金矿工(Java)DFS
题目: 5215. 黄金矿工
你要开发一座金矿,地质勘测学家已经探明了这座金矿中的资源分布,并用大小为 m * n
的网格 grid
进行了标注。每个单元格中的整数就表示这一单元格中的黄金数量;如果该单元格是空的,那么就是 0
。
为了使收益最大化,矿工需要按以下规则来开采黄金:
- 每当矿工进入一个单元,就会收集该单元格中的所有黄金。
- 矿工每次可以从当前位置向上下左右四个方向走。
- 每个单元格只能被开采(进入)一次。
- 不得开采(进入)黄金数目为
0
的单元格。 - 矿工可以从网格中 任意一个 有黄金的单元格出发或者是停止。
示例 1:
输入:grid = [[0,6,0],[5,8,7],[0,9,0]]
输出:24
解释:
[[0,6,0],
[5,8,7],
[0,9,0]]
一种收集最多黄金的路线是:9 -> 8 -> 7。
示例 2:
输入:grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
输出:28
解释:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
一种收集最多黄金的路线是:1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7。
提示:
1 <= grid.length, grid[i].length <= 15
0 <= grid[i][j] <= 100
- 最多 25 个单元格中有黄金。
题解:
此题采用 深度优先搜索(DFS) 可以完美解决。毕竟图不是很大。
dfs
方法返回的是已开采的最大收益,即开采完当前位置即其周围的单元格后的最大收益。
时间复杂度:
空间复杂度:
Java:
class Solution {
int[][] dir = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };// 上下左右
public int getMaximumGold(int[][] grid) {
boolean[][] visit = new boolean[grid.length][grid[0].length];
int ret = 0;// 黄金数量
for (int r = 0; r < grid.length; ++r) {
for (int c = 0; c < grid[r].length; ++c) {
ret = Math.max(ret, dfs(grid, r, c, 0, visit));
}
}
return ret;
}
int dfs(int[][] grid,
int row,
int col,
int earn, // 收益,即已开采的黄金数量
boolean[][] visit) {
if (row < 0 || row >= grid.length) {// 行越界
return earn;
}
if (col < 0 || col >= grid[row].length) {// 列越界
return earn;
}
if (grid[row][col] == 0) {// 当前位置无黄金
return earn;
}
if (visit[row][col]) {// 已开采过
return earn;
}
visit[row][col] = true;// 设置当前位置为已开采
earn += grid[row][col];// 收益增加
int ret = earn;
for (int[] d : dir) {// 遍历周围的四个方向
int r = row + d[0];
int c = col + d[1];
ret = Math.max(ret, dfs(grid, r, c, earn, visit));
}
visit[row][col] = false;// 恢复当前位置
return ret;// 开采当前位置以及周围位置后的最大收益
}
}
LeetCode 5215. 黄金矿工(Java)DFS的更多相关文章
- leetcode-157周赛-5215黄金矿工
题目描述: 方法一:dfs class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: maxx = 0 R, ...
- [leetcode] 39. 组合总和(Java)(dfs、递归、回溯)
39. 组合总和 直接暴力思路,用dfs+回溯枚举所有可能组合情况.难点在于每个数可取无数次. 我的枚举思路是: 外层枚举答案数组的长度,即枚举解中的数字个数,从1个开始,到target/ min(c ...
- [leetcode] 37. 解数独(Java)(dfs,递归,回溯)
37. 解数独 1A 这个题其实15分钟左右就敲出来并且对了...但是由于我输错了一个数..导致我白白debug一个多小时.. 没啥难度,练递归-dfs的好题 class Solution { pri ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- [Tyvj Aug11] 黄金矿工
传送门 Description 黄金矿工是一个经典的小游戏,它可以锻炼人的反应能力.该游戏中,可以通过“挖矿”获得积分并不断升级.玩家可以在线玩flash版黄金矿工,也可以下载后玩单机版黄金矿工.目前 ...
- 洛谷3961 [TJOI2013]黄金矿工
题目描述 小A最近迷上了在上课时玩<黄金矿工>这款游戏.为了避免被老师发现,他必须小心翼翼,因此他总是输.在输掉自己所有的金币后,他向你求助.每个黄金可以看做一个点(没有体积).现在给出你 ...
随机推荐
- cronicle docker 运行试用
Cronicle 是一款基于nodejs 开发的分布式任务调度工具,包含了比较全的UI,使用也比较简单,为了 方便学习,简单制作了一个docker 镜像,方便使用 Dockerfile FROM ...
- 使用openrc 管理容器中的服务
对于后台任务一般是不建议在容器中运行的,但是如果我们为了简化应用的部署,可能会使用后台任务进行服务的管理,类似的 工具很多,supervisor,systemd , init.d 同时对于docker ...
- JS的ES6的Symbol
一.Symbol 1.什么是Symbol: Symbol是ES6新添加的原始类型(ES5已有原始数据类型:String,Number,boolean,function,undefined,object ...
- windows下redis的配置文件(redis.windows.conf)
#redis的配置 #Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize yes #当Redis以守护进程方式运行时,Redis默认会把pid写入 ...
- C复习---动态内存分配
原型extern void *malloc(unsigned int num_bytes);头文件#include <stdlib.h>#include <malloc.h>函 ...
- Python并发请求之requests_future模块使用
# -*- coding: utf-8 -*- # @Time : 2019-12-09 10:00 # @Author : cxa # @File : demo.py # @Software: Py ...
- 【Beta】Scrum meeting 5
目录 写在前面 进度情况 任务进度表 Beta-1阶段燃尽图 遇到的困难 照片 commit记录截图 小程序前端仓库 后端代码仓库 技术博客 写在前面 例会时间:5.9 22:30-23:30 例会地 ...
- [技术博客]微信小程序开发中遇到的两个问题的解决
IDE介绍 微信web开发者工具 前端语言 微信小程序使用的语言为wxml和wss,使用JSON以及js逻辑进行页面之间的交互.与网页的html和css略有不同,微信小程序在此基础上添加了自己的改进, ...
- linux Ubuntu Centos 增加 TCP 连接数
https://blog.csdn.net/c359719435/article/details/80300433 查看max conn: cat /proc/sys/net/core/somaxco ...
- paddlepaddle如何预加载embedding向量
使用小批量数据时,模型容易过拟合,所以需要对全量数据进行处理,我是用的是word2vec训练的词向量. 那么训练好对词向量如何加载呢? #!/usr/bin/env python # -*- codi ...