【LeetCode】174. Dungeon Game 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/dungeon-game/
题目描述
The demons had captured the princess § 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.
题目大意
骑士救公主,骑士在左上角,公主在右下角,每个房间都会有个血量变化,如果血量<=0,立马死去。问骑士出发的时候带了多少血,才能成功就到公主。每个移动只能向右或者向下。
解题方法
动态规划
如果做了64. Minimum Path Sum,发现还有点类似,不过64题是只有正数,而这个题里面有正负数。
我们同样的想到了动态规划,但是一般的题目里面都是告诉了初始状态,但是这个题目要求初始状态,所以难度加大了。其实换一个思维,要求初始状态血量最小值,就是要到达右下角之后血量为1,即我们知道了结束状态是1。那么,我们需要换一个思路,就是从右下角出发到达左上角,需要的最少血量。
我们定义动态规划二维数组dp[i][j]表示,从右下角移动到i,j位置,需要的最少血量。那么,我们在最右边和最下边在来一层无穷大,表示到达这些位置是不可能的,但是,我们需要在公主的右边和下边的格子里血量初始化为1,即我们移动到这两个位置需要最少血量是1.
| . | . | . | . |
|---|---|---|---|
| -2 (K) | -3 | 3 | INT_MAX |
| -5 | -10 | 1 | INT_MAX |
| 10 | 30 | -5 ( P ) | 1 |
| INT_MAX | INT_MAX | 1 | INT_MAX |
有个简单的状态转移能看出来:dp[i][j] = min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j],即每个位置需要的最少血量是左边和下边的需要的最少血量 - 当前的房间的扣血量。这里需要解释一下为什么是减号:骑士到达这个屋子如果能活下去,那么需要的血量是多少呢?必须减掉扣血量才是当前需要的血量啊!
另外我们注意到,上面的dp[i][j]有可能是0或者负数,这种情况下骑士是没法存活的,骑士的最小血量要求是1,所以有最终的状态转移版本:
dp[i][j] = max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]);
C++代码如下:
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
const int M = dungeon.size(), N = dungeon[0].size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1, INT_MAX));
dp[M][N - 1] = dp[M - 1][N] = 1;
for (int i = M - 1; i >= 0; i--) {
for (int j = N - 1; j >= 0; j--) {
dp[i][j] = max(1, min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]);
}
}
return dp[0][0];
}
};
日期
2018 年 12 月 29 日 —— 2018年剩余电量不足1%
【LeetCode】174. Dungeon Game 解题报告(Python & C++)的更多相关文章
- leetcode 174. 地下城游戏 解题报告
leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- 使用BRAKER2进行基因组注释
来自:https://www.jianshu.com/p/e6a5e1f85dda 使用BRAKER2进行基因组注释 BRAKER2是一个基因组注释流程,能够组合GeneMark,AUGUSTUS和转 ...
- ZAQI
mysql> CREATE TABLE emploee ( -> name CHAR(64) NOT NULL, -> email CHAR(64), -> password ...
- C++栈溢出
先看一段代码 #include<iostream> using namespace std; #define n 510 void sum(int a,int b) { cout<& ...
- (数据科学学习手札132)Python+Fabric实现远程服务器连接
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 日常工作中经常需要通过SSH连接到多台远程 ...
- WebRTC视频分辨率设置
前面我们能够打开摄像头.getUserMedia()时会传入参数,在参数里我们可以指定宽高信息.通过宽高参数控制输出的视频分辨率. html 在页面上摆放一些元素,下面是主要部分 <div id ...
- 日常Java 2021/10/21
Java Iterator(迭代器) 如果需要使用iterator类需要从java.util包中引入它 Java Iterator不是一个集合,它是一种访问集合的方法,用于迭代ArrayList和Ha ...
- 大数据学习day17------第三阶段-----scala05------1.Akka RPC通信案例改造和部署在多台机器上 2. 柯里化方法 3. 隐式转换 4 scala的泛型
1.Akka RPC通信案例改造和部署在多台机器上 1.1 Akka RPC通信案例的改造(主要是把一些参数不写是) Master package com._51doit.akka.rpc impo ...
- cookie规范(RFC6265)翻译
来源:https://github.com/renaesop/blog/issues/4 RFC 6265 要点翻译 1.简介 本文档定义了HTTP Cookie以及HTTP头的Set-Cookie字 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(五)-文件管理初步介绍
其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...
- 规范——Java后端开发规范
Java后端开发规范 一.技术栈规约 二.命名规范 三.Java代码规范(注释规范.异常与日志.代码逻辑规范) 四.Mybatis与SQL规范 五.结果检查(单元测试及代码扫描) 六.安全规范 一.技 ...