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,但是如何用DP,刚开始的想法是,

1、用两个数组,一个数组记录到当前位置需要的最小血量min[len],另一个是在最小血量的情况下路径上的数的和sum[len]。

2、第一行因为只能一直向右走,所以很简单。

3、从第二行开始,有两种选项,从上面来还是从左边来(第一个单独算),然后比较这两种选项需要的最小血量,选择较小的那条路。

4、在两种选项的血量一样的时候,选择sum较大的走。

但是这样会出错。

[[1,-3,3],[0,-2,0],[-3,-3,-3]]这一组就会出错,因此这样是不对的。局部最优不等于全局最优。

public class Solution {//错误代码
public int calculateMinimumHP(int[][] dungeon) {
if (dungeon.length == 1 && dungeon[0].length == 1){
if (dungeon[0][0] > 0){
return 1;
} else {
return -dungeon[0][0]+1;
}
}
int len = dungeon[0].length;
int[] min = new int[len];
int[] sum = new int[len];
int num = dungeon[0][0];
min[0] = Math.max(1, -num + 1);
sum[0] = num;
for (int i = 1; i < len; i++){
num += dungeon[0][i];
sum[i] = num;
min[i] = Math.max(-num + 1, min[i - 1]);
}
int flag = 0;
int flag2 = 1;
for (int i = 1;i < dungeon.length; i++){
sum[0] += dungeon[i][0];
min[0] = Math.max(min[0], -sum[0] + 1);
for (int j = 1;j < len; j++){
if (min[j - 1] < min[j] || (min[j - 1] == min[j] && sum[j - 1] > sum[j])){
sum[j] = sum[j - 1] + dungeon[i][j];
min[j] = Math.max(min[j - 1], -sum[j] + 1);
} else {
sum[j] += dungeon[i][j];
min[j] = Math.max(min[j], -sum[j] + 1);
}
}
}
return min[len - 1];
}
}

2、反过来,从结尾开始,表示当前位置到结尾最少需要的血量

public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
if (dungeon.length == 1 && dungeon[0].length == 1){
if (dungeon[0][0] > 0){
return 1;
} else {
return -dungeon[0][0] + 1;
}
}
int len = dungeon[0].length;
int row = dungeon.length;
int[] min = new int[len];
min[len - 1] = -dungeon[row - 1][len - 1] + 1;
min[len - 1] = Math.max(min[len - 1], 1);
for (int i = len - 2; i >= 0; i--){
min[i] = min[i + 1] - dungeon[row - 1][i];
min[i] = Math.max(min[i], 1);
}
for (int i = row - 2; i >= 0; i--){
min[len - 1] = min[len - 1] - dungeon[i][len - 1];
min[len - 1] = Math.max(min[len - 1], 1);
for (int j = len - 2; j >= 0; j--){
min[j] = Math.min(min[j + 1], min[j]) - dungeon[i][j];
min[j] = Math.max(min[j], 1);
}
}
return min[0];
}
}

✡ leetcode 174. Dungeon Game 地牢游戏 --------- java的更多相关文章

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

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

  2. [leetcode]174. Dungeon Game地牢游戏

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

  3. [LeetCode] Dungeon Game 地牢游戏

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

  4. Java for LeetCode 174 Dungeon Game

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

  5. 174 Dungeon Game 地下城游戏

    一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...

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

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

  7. Leetcode#174 Dungeon Game

    原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...

  8. LeetCode 174. Dungeon Game (C++)

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

  9. leetcode 174. 地下城游戏 解题报告

    leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...

随机推荐

  1. listview指定某item的点击效果

    需求:listview的某些item能够点击,需要点击效果,有些item不能点击,需要屏蔽点击效果. 实现: 1.layout: <ListView android:id="@+id/ ...

  2. 【转】修改LINUX时间

    命令格式为: date -s 时间字符串 例如只修改系统的日期,不修改时间(时分秒) date -s 2012-08-02 或只修改时间不修改日期 date -s 10:08:00 当然也可以同时修改 ...

  3. 不使用ASP.NET中的服务器控件将如何上传文件?

    遇到文件的上传时,可能会有大部分的开发者喜欢使用服务器控件,虽然很方便,但是却不能很好的控制,不具灵活性. 现给出例子,使用html标签语言灵活的控制文件的上传. 1.html部分 <input ...

  4. Nutch2.x

    http://www.micmiu.com/opensource/nutch/nutch2x-tutorial/

  5. Javascript DOM编程艺术 语法部分

    1.变量,可以变化的东西我们称为变量,随着年龄的增大,我们的age不断变大 2.Javascript变量声明用var,可以不声明变量类型.尽量声明为一个字符串字面量. 3.弱类型:要求程序员必须明确的 ...

  6. 也来说说C/C++里的volatile关键字

    去年年底的样子,何登成写了一篇关于C/C++ volatile关键字的深度剖析blog(C/C++ Volatile关键词深度剖析).全文深入分析了volatile关键字的三个特性.这里不想就已有内容 ...

  7. scala学习----柯里化

    1.鸭子类型,走起来像鸭子,叫起来像鸭子,就是鸭子.函数中使用{ def close(): Unit }作为参数类型,因此任何含有此函数的类都可以作为参数传递.好处是不必使用继承特性. def wit ...

  8. Maven联网问题

    关于Maven联网问题 关于如何找到Maven默认的本地仓库问题 修改Maven默认本地仓库 找到 <localRepository>/path/to/local/repo</loc ...

  9. shopex 小知识

    产品链接: http://www.--/product-172.html 中间的数字代表  sdb_goods 表中 的 goods_id  ... 表示数据库里的产品 id. 分类链接: http: ...

  10. linux下包不重新编译php安装openssl 扩展

    今天在刚装好的centos上安装composer,老是报错,提示不安全的,后来发现是由于https的缘故,需要openssl,可能是开始装php的时候没有安装此扩展,网上有好多方案,一开始我认为只有重 ...