原题链接在这里: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. Ajax注册验证用户名是否存在 ——引自百度经验

    Ajax注册验证用户名是否存在 http://jingyan.baidu.com/article/a948d6515fdf870a2dcd2e85.html

  2. Ubuntu 下误修改用户组导致sudo命令无效

    1.手贱地修改了当前用户的权限组,导致sudo命令无法使用,且root用户的密码尚未修改,登陆不了root用户. 2.切换到recoverymode ,使用root用户登陆 3.执行mount -o ...

  3. linux用户和组管理

    添加组groupadd sftp 把用户mysftp加入组sftp中:gpasswd -a mysftp sftp 把用户mysftp加入组sftp中:usermod -a -G sftp mysft ...

  4. linux的信号机制

    软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.进程之间可以互相通过系统调用kill发送软中断信号.内核也可以因为内部事件而给进程发送信号,通知进程发生了某个事件.注意,信号只是用 ...

  5. C#中的托管堆和堆栈

    托管堆(Heap)和堆栈(Stack)是内存的逻辑划分.   栈 堆 连续性 连续 不连续 有序性 后进先出 无序 内存管理 操作系统自动释放 GC或人工 存放类型 值类型/引用 引用类型 注:内存格 ...

  6. [听听音乐]love is blue

    在朋友圈里听到这首歌,好像是中央台天气预报用过的背景音乐.百度了一下,大致如下: 1967年,在维也纳举行的欧洲电视歌唱大赛,卢森堡歌手薇基·琳德洛丝(Vicky Leandros)演唱了一首由彼埃尔 ...

  7. 综合支撑【恶灵附身 Psycho Break】的世界观的概念艺术

    综合支撑[恶灵附身  Psycho Break]的世界观的概念艺术 三上真司监督提出的,是对着重表现讲述内心恐怖的哪个世界观的创作和统一做了很大贡献的概念艺术. 这里以他经手的艺术为例,来看看稀少的恐 ...

  8. but this usually doesn’t gain you anything.

    High Performance My SQL, Third Edition Date and Time Types My SQL has many types for various kinds o ...

  9. register instruction pointer

    Computer Science An Overview _J. Glenn Brookshear _11th Edition We have already encountered the conc ...

  10. The "get" method should be used when the form is idempotent---正交的两个概念---

    https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.1 17.13.1 Form submission method The me ...