作者: 负雪明烛
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. 汽车C2M模式综述

  2. php导出pdf,dompdf中文字体乱码解决办法(特别是代码迁移引起的乱码)

    dompdf\lib\fonts\dompdf_font_family_cache.php记住这个文件里面存放的是字体生成的缓存,迁移时如果覆盖了这个文件会导致乱码而且很难找到出错的地方,相信我... ...

  3. 1.TwoSum-Leetcode

    #include<iostream> #include<algorithm> #include<map> using namespace std; class So ...

  4. javaWeb - 1 — servlet — 更新完毕

    1.先来聊一些javaWeb相关的知识 简单了解一下:web的发展史 1).web就是网页的意思嘛 2).web的分类 (1).静态web 使用HTML.CSS技术,主要包括图片和文本 优点:简单,只 ...

  5. A Child's History of England.2

    They made boats of basket-work, covered with the skins of animals, but seldom, if ever, ventured far ...

  6. 纯CSS圆环与圆

    1. 两个标签的嵌套: <div class="element1"> <div class="child1"></div> ...

  7. Java对象的创建过程:类的初始化与实例化

    一.Java对象创建时机 我们知道,一个对象在可以被使用之前必须要被正确地实例化.在Java代码中,有很多行为可以引起对象的创建,最为直观的一种就是使用new关键字来调用一个类的构造函数显式地创建对象 ...

  8. python爬取实习僧招聘信息字体反爬

    参考博客:http://www.cnblogs.com/eastonliu/p/9925652.html 实习僧招聘的网站采用了字体反爬,在页面上显示正常,查看源码关键信息乱码,如下图所示: 查看网页 ...

  9. Jenkins配置java项目

    目录 一.场景介绍 二.项目配置 配置插件 配置项目 一.场景介绍 在部署完Jenkins后,需要将现有的maven项目(Jenkis的开源插件),放到Jenkins上,用于自动化运维的改造. 项目地 ...

  10. MySQL信息系统函数