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. tsql 循环id读取

    declare @IDList as varchar(max) declare @ID as int declare @i as int set @IDList='' )) + ',' from ta ...

  2. js将UTC时间转化为当地时区时间

    js将UTC时间转化为当地时区时间(UTC转GMT)   我们在进行网站开发的时候有可能会涉及到国外的用户或者用户身在国外,这时就会存在时差问题,比如说我们在中国的时间是08:00,但是此时韩国的时间 ...

  3. CUDA C Programming Guide 在线教程学习笔记 Part 5

    附录 A,CUDA计算设备 附录 B,C语言扩展 ▶ 函数的标识符 ● __device__,__global__ 和 __host__ ● 宏 __CUDA_ARCH__ 可用于区分代码的运行位置. ...

  4. oracle11gr2笔记(一)

    一,使用scoot用户被锁.解决办法:(http://ciiiso.blog.51cto.com/8779682/1432869/) 二,使用root用户登录系统无法sqlplus,提示说permis ...

  5. node 图片上传功能

    node 代码: var http = require("http"); var express = require('express') app = express(), for ...

  6. Pycharm远程连接服务器(windows下远程修改服务器代码)

    1.写在前面     之前一致用putty,ssh,修改代码,或者本地修改,上传到服务器,各种不爽,现在改用xshell,但是有时候还是不方便感觉,于是自己配置了远程连接pycharm,这样不用总是到 ...

  7. jd-eclipse反编译插件的在线安装和使用

    jd-eclipse反编译插件的在线安装和使用 JD-Eclipse是一个Eclipse平台的插件.它允许您调试所有的Java源代码,有了它,以后调试的时候ctrl键就可以一键到底啦.下面简单说说ec ...

  8. ImageIO(图像处理)

    1.通过ImageIO的read和writer,对图像文件进行处理. BufferedImage buffImage = ImageIO.read(file); // 将图像输出到Servlet输出流 ...

  9. 从底层谈WebGIS 原理设计与实现(二):探究本质,WebGIS前端地图显示之地图比例尺换算原理

    从底层谈WebGIS 原理设计与实现(二):探究本质,WebGIS前端地图显示之地图比例尺换算原理 作者:naaoveGI…    文章来源:http://www.cnblogs.com/naaove ...

  10. 【344】Jupyter relevant problems

    参考:Jupyter Notebook Tutorial: The Definitive Guide 参考:ipython notebook 如何修改一开始打开的文件夹路径? Ref: Install ...