作者: 负雪明烛
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:

  1. The knight’s health has no upper bound.
  2. 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++)的更多相关文章

  1. leetcode 174. 地下城游戏 解题报告

    leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. 全基因组选择育种(GS)简介

    全基因组选择(Genomic selection, GS)是一种利用覆盖全基因组的高密度标记进行选择育种的新方法,可通过早期选择缩短世代间隔,提高育种值(Genomic Estimated Breed ...

  2. R语言与医学统计图形-【28】ggplot2扩展包ggrepel、ggsci、gganimate、ggpubr

    ggplot2绘图系统--扩展包ggrepel.ggsci.gganimate.ggpubr等 部分扩展包可在CRAN直接下载,有些需借助devtools包从Github下载. 1. ggrepel包 ...

  3. Atom编辑器速查

    简介 Atom 是 Github 开源的文本编辑器,相当于半个IDE.其特点如下: (1)免费开源,多平台支持(Windows.Mac.Linux): (2)界面美观.现代化,使用舒适: (3)多文件 ...

  4. Python—python2.7.5升级到2.7.14或者直接升级到3.6.4

    python2.7.5升级到2.7.14 1.安装升级GCC yum install -y gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2 ...

  5. 深入理解动态规划DP

    通过最近对于一些算法题的思考,越来越发现动态规划方法的在时间上高效性,往往该问题可以轻松的找到暴力破解的方法,其时间复杂度却不尽人意.下面来看看几个常见的动态规划思路的经典问题 例一.有一段楼梯有10 ...

  6. 日常Java 2021/10/26

    HashSet基于HashMap来实现的,是一个不允许有重复元素的集合.HashSet 允许有null 值. HashSet是无序的,即不会记录插入的顺序. HashSet不是线程安全的,如果多个线程 ...

  7. acclaim

    欲学acclaim,先学claim.即使学会claim,未必记住acclaim. [LDOCE] claim的词源是cry out, shoutverb:1. state that sth is tr ...

  8. Spark基础:(七)Spark Streaming入门

    介绍 1.是spark core的扩展,针对实时数据流处理,具有可扩展.高吞吐量.容错. 数据可以是来自于kafka,flume,tcpsocket,使用高级函数(map reduce filter ...

  9. Codeforces Round #754 (Div. 2) C. Dominant Character

    题目:Problem - C - Codeforces 如代码,一共有七种情况,注意不要漏掉  "accabba"  , "abbacca"  两种情况: 使用 ...

  10. android studio 使用 aidl(二)异步回调

    基础使用请移步 android studio 使用 aidl (一) 首先建立在server端建立两个aidl文件 ITaskCallback.aidl 用于存放要回调client端的方法 // IT ...