Dynamic Programming - 174. Dungeon Game
174. Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN
.
-2 (K) |
-3 |
3 |
---|---|---|
-5 |
-10 |
1 |
10 |
30 |
-5 (P) |
Note:
- The knight's health has no upper bound.
- Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
思路:
题目意思是有一个骑士在网格的左上角,一个公主在右下角,每个网格中的数字如果是正代表给骑士加血,如果是负数就说明要扣血,题目要求找出骑士在开始的时候需要多少血量,才能救到公主。在做dp的题目的时候其实最难的就是抽象子问题,找出状态转移方程,剩下的都差不多。 这一题中最后一步是走到右下角,扣完网格中的血之后大于零就能就到公主,而最后一步只能从上面走下来和左边走到右边。所以子问题就是上一步留下的血量在当前网格扣完还能大于零。状态转移方程就是
dp[i][j] = min(max(dp[i+1][j] - dungeon[i][j], 1), max(dp[i][j+1] - dungeon[i][j], 1))
, 边界条件就是最左边一列和最后行不能通过状态转移方程求出。初始值就是右下角格子扣完血和1的最小值(可以用1减去当前网格然后和1求最大值)。
代码:
go:
func calculateMinimumHP(dungeon [][]int) int {
m, n := len(dungeon), len(dungeon[0])
for i := m - 1; i >= 0; i-- {
for j := n -1; j >=0; j-- {
if i == m - 1 && j == n - 1 {
dungeon[i][j] = max(1 - dungeon[i][j], 1)
} else if i == m - 1 && j != n - 1 { // 只能往右,反推即为左
dungeon[i][j] = max(dungeon[i][j+1] - dungeon[i][j], 1)
} else if i != m -1 && j == n - 1 {
dungeon[i][j] = max(dungeon[i+1][j] - dungeon[i][j], 1)
} else {
dungeon[i][j] = min(max(dungeon[i+1][j] - dungeon[i][j], 1), max(dungeon[i][j+1] - dungeon[i][j], 1))
}
}
}
return dungeon[0][0]
}
func min(i, j int) int {
if i < j {
return i
}
return j
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
- 图文并茂详解 SQL JOIN
- 自定义 hadoop MapReduce InputFormat 切分输入文件
- Hadoop MapReduce 二次排序原理及其应用
- MySQL Tips【Updating】
- Meltdown、Spectre攻击---CPU乱序执行和预测执行导致的安全问题
- WordPress 4.6远程代码执行漏洞(CVE-2016-10033)复现环境搭建指南
- 相似文档查找算法之 simHash 简介及其 java 实现
- Hadoop 中利用 mapreduce 读写 mysql 数据
- Android O中对TEE加解密算法的新要求
- storm 原理简介及单机版安装指南
- Python Tips, Tricks, and Hacks
- 英特尔放出Linux微代码以修复Meltdown和Spectre漏洞
- python基础(5):深入理解 python 中的赋值、引用、拷贝、作用域
- Linux SSH密码暴力破解技术及攻防实战
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 2.1 The Python Interpreter(python解释器)
- Linux系统—Centos7安装教程
- 2.2 IPython基础
- Linux系统crond、rsync、打包备份数据操作实战
- 2.3 Python语言基础
- 开发需求
- Linux系统组建SVN服务器
- 面向对象学习
- 常用模块
- 代理错误[WinError 10061]
- Linux系统JDK+Tomcat环境安装布署过程
- Python version 3.6 required, which was not found in the registry错误解决
- LNMP架构应用实战——Nginx服务介绍与安装
- 使用tidylib解决不规则网页问题
- LNMP架构应用实战——Nginx服务配置文件介绍