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
}
- [接口测试 - 基础篇] 09 其实吧,读写csv格式也是要掌握的
- 【源码分享】机器学习之Python支持向量机
- [接口测试 - 基础篇] 08 封装个基本的excel解析类
- 关关的刷题日记10——Leetcode 1. Two Sum 方法1
- BZOJ 2463: [中山市选2009]谁能赢呢?(新生必做的水题)
- 10个Python面试常问的问题
- 关关的刷题日记11——Leetcode 1. Two Sum 方法2、3
- 计蒜客:百度的科学计算器(简单)【python神解】
- 关关的刷题日记12——Leetcode 189. Rotate Array 方法1、2、3
- 关关的刷题日记13——Leetcode 414. Third Maximum Number
- 关关的刷题日记14——Leetcode 167. Two Sum II - Input array is sorted
- 51Nod 1083 矩阵取数问题(矩阵取数dp,基础题)
- 【专知-关关的刷题日记15】Leetcode 27. Remove Element 方法1、2、3
- [接口测试 - 基础篇] 07 来来来,一起读写excel玩玩之一
- 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 数组属性和方法
- Mybatis插件扩展以及与Spring整合原理
- 你所不知道的Spring的@Autowired实现细节
- modbus-RTU-crc16——c语言
- KEIL 生成 Binaxf 文件
- Istio可观测性
- 大点干!早点散----------使用Haproxy搭建web群集
- 嵌入式系统FreeRTOS — 互斥信号量
- 面试题系列第6篇:JVM字符串常量池及String的intern方法详解?
- 机器学习(一)——K-近邻(KNN)算法
- 机器学习(二)——K-均值聚类(K-means)算法
- windows下matplotlib的安装
- Python中常见的数据类型总结
- stm32中阻塞模式和非阻塞模式 in blocking mode 与 in non-blocking mode区别
- 机器学习(四)—决策树
- 机器学习(五)—朴素贝叶斯