原题链接在这里: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. Grasshopper 2.0 MP Color FireWire 1394b (Sony ICX274)

        相机参数如下,参见这里: Resolution 1624 x 1224 Frame Rate 30 FPS Megapixels 2.0 MP Chroma Color Sensor Name ...

  2. ps插件安装

    CutAndSliceMe.zxp 切图插件安装,下载后改为zip后缀,再解压后 复制文件夹到(PS软件安装目录)PhotoshopCC\Plug-ins\Panels文件夹下面

  3. php广告显示设置存放记录的目录代码

    <?php #########随机广告显示########## function myads(){ $dir="ads"; #设置存放记录的目录 //$dir="a ...

  4. metasploit-post模块信息

    Name                                             Disclosure Date  Rank    Description ----           ...

  5. Git Shell使用笔记

    1,首次打开Git shell错误(以前打开过gethub客户端) 警告: git command could not be found. Please create an alias or add ...

  6. Apache Spark源码走读之11 -- sql的解析与执行

    欢迎转载,转载请注明出处,徽沪一郎. 概要 在即将发布的spark 1.0中有一个新增的功能,即对sql的支持,也就是说可以用sql来对数据进行查询,这对于DBA来说无疑是一大福音,因为以前的知识继续 ...

  7. Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50

    Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50 分类: 系统运维 查找包含BOM头的文件,命令如下: 点击(此处)折叠或打开 grep -r -I -l ...

  8. jquery动态加载问题

    对于append的元素,原有的方法不生效 解决:用on方法 找到的:http://www.zhidao91.com/jquery-html-live-on/ 解决使用jQuery采用append添加的 ...

  9. 20145317彭垚《Java程序设计》实验二

    20145317<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...

  10. the OS maintains a number of queues

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To do its job, the OS ...