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. Rest架构以及什么是Restful

    关于Rest的内容,在网上开了好多文章~ 下面我就把一些关于Rest经典的链接发出来,大家可以参考一下~ 1.什么是Rest和Restful? 怎样用通俗的语言解释什么叫 REST,以及什么是 RES ...

  2. Maven 添加jdk编译插件

    问题描述: 默认情况下,eclipse的maven项目使用jdk1.5编译,而我们的jdk为1.8每次更改jdk1.5之后,只要maven项目已更新,eclipse就会自动的回到jdk1.8结局方法: ...

  3. PHP mysqli 增强 批量执行sql 语句的实现代码

    本篇文章介绍了,在PHP中 mysqli 增强 批量执行sql 语句的实现代码.需要的朋友参考下. mysqli 增强-批量执行sql 语句 <?php //mysqli 增强-批量执行sql ...

  4. JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  5. 学习MongoDB 七: MongoDB索引(索引基本操作)(一)

    一.简介 在MongoDB建立索引能提高查询效率,只需要扫描索引只存储的这个集合的一小部分,并只把这小部分加载到内存中,效率大大的提高,如果没有建立索引,在查询时,MongoDB必须执行全表扫描,在数 ...

  6. Sublime Text 3 个人使用总结

    待更新 Sublime Text 3\Packages\FileHeader\template\header

  7. python list unicode转中文显示

    [u'\u773c', u'\u8179\u90e8', u'\u4e94\u5b98', u'\u53e3\u8154', u'\u8179\u90e8', u'\u53e3\u8154'] str ...

  8. Github入门 - Github基本使用及Github桌面版使用

    知识内容: 1.版本控制 2.Git介绍 3.Github介绍及基本使用 4.Github桌面版介绍及安装 5.Github桌面版基础使用 6.Github桌面版进阶使用 参考: http://www ...

  9. PCB的初次窥探

    第一次画PCB经常用到的知识点 鼠标拖动+X      :左右转动(对称) +space:90度转动 +L      :顶层与底层的切换 Ctrl+M:测量 J + C:查找原件 交叉探针+原理图(P ...

  10. spring boot 自定义异常

    1.创建一个异常: public class LdapQueryException extends Exception { private Integer code; private String m ...