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)

题意:

骑士(左上角)要救出公主(右下角),只能从起点开始往右或者往下走。每踩一个格子可能失血(或加血)。骑士能活着(至少保持一滴血不死)救出公主的初始血量至少为多少?

这个题类似64. Minimum Path Sum , 但更为复杂一点的是,从右下角反推左上角,且至少保持一滴血不死。

思路:

-2(K)     -3         3
-5 -10 1
10 30 -5(P)

初始化,

int[][] dp = new int[row][col];   row = grid.length, col = grid[0].length

dp[row-1][col-1] = max( 1 - grid[row-1][col-1],  1)   若grid[row-1][col-1] 这格是加血,则反推骑士踩这格之前的血量为1(至少保持一滴血不死)即可。

若grid[row-1][col-1] 这格是减血,则反推骑士踩这格之前的血量为1 + 减血血量,即 1 - [row-1][col-1]

?     ?    ?
? ? ?
? ?

是否需要预处理最后一个row: dp[row-1][col],因为矩阵中间的dp[row][col]既可能来自下方,也可能来自右方,所以先预处理仅来自右方的每格结果

是否需要预处理最后一个col:dp[row][col-1],   因为矩阵中间的dp[row][col]既可能来自下方,也可能来自右方,所以先预处理仅来自下方的每格结果


代码:

 class Solution {
public int calculateMinimumHP(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
int[][]dp = new int[row][col];
dp[row-1][col-1] = Math.max (1- grid[row-1][col-1], 1) ; for(int i = row-2; i>=0 ; i--){
dp[i][col-1] = Math.max (dp[i+1][col-1] - grid[i][col-1] , 1) ;
}
for(int j = col-2; j>=0 ; j--){
dp[row-1][j] = Math.max (dp[row-1][j+1] - grid[row-1][j] , 1) ;
} for(int i = row-2; i>=0 ; i--){
for(int j = col-2; j>=0 ; j--){
int down = Math.max (dp[i+1][j] - grid[i][j] , 1) ;
int right = Math.max(dp[i][j+1] - grid[i][j], 1);
dp[i][j] = Math.min(down, right);
}
}
return dp[0][0];
}
}

[leetcode]174. Dungeon Game地牢游戏的更多相关文章

  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 地牢游戏 --------- java

    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. leetcode@ [174] Dungeon Game (Dynamic Programming)

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

  5. 174 Dungeon Game 地下城游戏

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

  6. Java for LeetCode 174 Dungeon Game

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

  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. php表达式

    表达式是PHP中一个重要的概念,可以把表达式理解为“任何有值的东西”.在本教程中涉及到表达式的语法,我们以“expr”来表示表达式. 下面就是一个表达式: $x > $y; 在上面的例子中,当$ ...

  2. python-pycharm中使用anaconda部署python环境

    pycharm中使用anaconda部署python环境 今天来说一下python中一个管理包很好用的工具anaconda,可以轻松实现python中各种包的管理.相信大家都会有这种体验,在pycha ...

  3. Sklearn与特征工程

    Scikit-learn与特征工程 “数据决定了机器学习的上限,而算法只是尽可能逼近这个上限”,这句话很好的阐述了数据在机器学习中的重要性.大部分直接拿过来的数据都是特征不明显的.没有经过处理的或者说 ...

  4. jap 事务总结

    参考: JPA事务总结 2010年4月13日 - 从表11-2中可以看出,对于不同的EntityManager类型与所运行的环境,所支持的事务类型是不一样的. 其中两种情况下最为简单,一种是容器托管的 ...

  5. net 编译报错:编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件

    1,报错提示: 编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件. 在生成过程中保存文件是危险的,这可能会在将来导致不正确的生成输出. 是否仍然继续签出? 2,原因:licenses.l ...

  6. BDE 退出历史 迁移至FireDAC

    BDE组件套TQuery\TTable\TStoreProce结束历时使命. ADO组件套也停止更新, 将来是FireDAC组件套的江湖 Migrating BDE Applications to F ...

  7. SpringMvc 获取ApplicationContext

    有时,我们不通过Controller层进入Service层,比如同步数据,任务,以及文件上传共通Handler对文件处理后保存数据等都会由一个非Controller类调用Service. 这时候如果n ...

  8. ABAP-HTML浏览器

  9. UI5-文档-4.19-Reuse Dialogs

    在此步骤中,我们将扩展重用概念,并在组件级别调用对话框. 在步骤16中,我们创建了一个对话框作为片段,以使其可跨视图或跨整个应用程序重用.但是我们将检索对话框实例和分别打开和关闭对话框实例的逻辑放置在 ...

  10. Rxjs 修改Observable 里的值

    有这么一个对象c$: Observable<any> 修改里边的值: 声明一个subject subject: Subject<any>; 在ngOnInit()中进行初始化 ...