原题链接在这里:https://leetcode.com/problems/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)

Notes:

    • 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, 需要保存当前格到右下格所需要的最小体力.

递归时, 是Math.min(走右侧最小体力,左下侧最小体力).

先出示右下角的点,再初始最后一行和最后一列.

Note:  1.最后返回的不是dp[0][0], 而是dp[0][0]加一,因为之前求得体力值的最小值是0, 但骑士的体力值必须是正数.

2. 之所以选择从后往前更新而不是从前往后更新是因为,从前往后更新时求得的局部最优不保证是全局最优。

AC Java:

 class Solution {
public int calculateMinimumHP(int[][] dungeon) {
if(dungeon == null || dungeon.length == 0 || dungeon[0].length == 0){
return 0;
} int m = dungeon.length;
int n = dungeon[0].length; int [][] dp = new int[m][n];
dp[m-1][n-1] = dungeon[m-1][n-1] < 0 ? -dungeon[m-1][n-1]:0;
for(int i = m-2; i>=0; i--){
dp[i][n-1] = dp[i+1][n-1] - dungeon[i][n-1] > 0 ? dp[i+1][n-1] - dungeon[i][n-1] : 0;
}
for(int j = n-2; j>=0; j--){
dp[m-1][j] = dp[m-1][j+1] - dungeon[m-1][j] > 0 ? dp[m-1][j+1] - dungeon[m-1][j] : 0;
} for(int i = m-2; i>=0; i--){
for(int j = n-2; j>=0; j--){
int cost = Math.min(dp[i+1][j], dp[i][j+1]);
dp[i][j] = cost - dungeon[i][j] > 0 ? cost - dungeon[i][j] : 0;
}
}
return dp[0][0]+1;
}
}

LeetCode Dungeon Game的更多相关文章

  1. [LeetCode] Dungeon Game 地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  2. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  3. leetcode@ [174] Dungeon Game (Dynamic Programming)

    https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...

  4. 【leetcode dp】Dungeon Game

    https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...

  5. [LeetCode] 174. Dungeon Game 地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  6. 【LeetCode】174. Dungeon Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  8. 【leetcode】Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...

  9. Java for LeetCode 174 Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

随机推荐

  1. JS中Date对象getYear()方法和getFullYear()方法区别

    释义 JavaScript getFullYear() 方法 getFullYear() 方法可返回一个表示年份的 4 位数字. getYear() 语法 dateObject.getFullYear ...

  2. GC 基础

    = GC 基础 ===================== JAVA堆的描述如下: 内存由 Perm 和 Heap 组成. 其中 Heap = {Old + NEW = { Eden , from, ...

  3. iOS开发之正则表达式

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达 ...

  4. QDir, QFileInfo 和 QDirIterator 区别

    这三个类相互有关联,但是有不尽相同,首先从名字上看,QDir 和 QDirIterator 是针对于文件目录的,也就是文件夹,我们知道,对于一个文件夹,可以包含很多文件,也可以包含其他文件夹,通常是一 ...

  5. new NABCD

    小组名称: 天天向上 项目名称:连连看 小组成员:王森(组长).胡丽娜.林莉.张政.张金生 新NABCD模型 N(需求) 传统的连连看有许多,玩法单一,感觉没意思,用户更希望连连看游戏增加更多的与众不 ...

  6. 页面静态化3 --- 伪静态技术之Apache的rewrite机制

      Apache的rewrite机制: 意思就是,你发送的地址,比如:http://localhost/news-id67.html会被Apache改写成http://localhost/news.p ...

  7. 验证码 mewebstudio/captcha

    composer require mews/captcha https://github.com/mewebstudio/captcha

  8. Nginx服务器

    什么是Nginx? Nginx是一种服务器软件,如同apache.tomcat.是一种高性能的HTTP和反向代理服务器以及代理邮件服务器.也就是说Nginx服务器可以发布网站,也可以负载均衡,还可以作 ...

  9. Android TextView 显示不全的自动补齐方式

    TextView在Android开发中用到的地方应该是很多的.很多时候,TextView会有一行显示不全被截取或者会换行.之前我的解决办法比较笨拙,定死TextView的一行字数长度,最后一个以省略号 ...

  10. EBS 资源路径

    /data03/DEV4/comn/java/cmcc/upload cd  $OA_HTML /data03/DEV4/comn/html 日志 $IAS_ORACLE_HOME/Apache/Js ...