作者: 负雪明烛
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. PL\SQL和PL/SQL Developer 12安装与配置

    安装: (1)在已有安装包的情况下,直接将安装包解压到文件夹下,注意不要解压缩到c:\programs Files(x86)的文件夹下,不能解压缩到有中文文件夹命名的文件夹下面 (2)没有安装包的情况 ...

  2. Spring Cloud中五花八门的分布式组件我到底该怎么学

    分布式架构的演进 在软件行业,一个应用服务随着功能越来越复杂,用户量越来越大,尤其是互联网行业流量爆发式的增长,导致我们需要不断的重构应用的结构来支撑庞大的用户量,最终从一个简单的系统主键演变成了一个 ...

  3. Typora数学公式输入指导手册

    Markdown 公式指导手册 公式大全的链接 https://www.zybuluo.com/codeep/note/163962#mjx-eqn-eqsample 目录 Markdown 公式指导 ...

  4. API 管理在云原生场景下的机遇与挑战

    作者 | 张添翼 来源 | 尔达Erda公众号 ​ 云原生下的机遇和挑战 标准和生态的意义 自从 Kubernetes v1.0 于 2015 年 7 月 21 日发布,CNCF 组织随后建立以来,其 ...

  5. 并发 并行 进程 线程 协程 异步I/O python async

    一些草率不精确的观点: 并发: 一起发生,occurence: sth that happens. 并行: 同时处理. parallel lines: 平行线.thread.join()之前是啥?落霞 ...

  6. 18. MYSQL 字符编码配置

    MYSQL 5.7版本的my.ini 在C盘隐藏文件夹下 C:\ProgramData\MySQL\MySQL Server 5.7 [client] default-character-set=ut ...

  7. Python计算期权隐含波动率

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. Black-Scholes 将期权价格描述为标的价格.行权价.无风险利率.到期时间和波动性的函数.  V ...

  8. RocketMQ集群搭建方式

    各角色介绍 Producer:消息的发送者:举例:发信者 Consumer:消息接收者:举例:收信者 Broker:暂存和传输消息:举例:邮局 NameServer:管理Broker:举例:各个邮局的 ...

  9. Postman 中 Pre-request Script 常用 js 脚本

    1. 生成一个MD5或SHA1加密的字符串str_md5,str_sha1 string1 = "123456"; var str_md5= CryptoJS.MD5(string ...

  10. 转 Android Lifecycle、ViewModel和LiveData

    转自:https://www.jianshu.com/p/982545e01d0a 1.概述 在I / O '17的时候,其中一个重要的主题是Architecture Components.这是一个官 ...