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. Nexus3 仓库搭建(基于Docker)

    Nexus Repository OSS 支持的仓库类型 安装方法(使用docker) 命令: sudo docker run -d \ --name nexus3 \ --restart=alway ...

  2. 长沙雅礼中学集训-------------------day2

    怎么说呢,今天的题特别的神奇,因为emmmmmm,T1看不懂(一直到现在还没有理解明白期望概率什么的),T2题面看不懂+扩展欧几里得求逆元怎么求我忘了,T3哇,终于看懂一题了,然而写了个50分的程序但 ...

  3. python之路day03

    1  复习计算机基础 计算机基础我们讲到完整的计算机系统包括了:应用程序,操作系统,硬件三部分.那么硬件又分为:cpu,内,和硬盘. 对于用户来说我们操作计算机是通过应用程序来间接控制计算机.当我们打 ...

  4. IDEA设置syso快捷键输出System.out.println();

    用Eclipse时间长了, 就习惯之前的快捷键! 当然, IDEA不愧是Java开发的”利器”! 写起代码就是一个字 – “爽”! 建议大家可以去尝试一下! 当然, 在IDEA中输出System.ou ...

  5. 经典算法 KMP算法详解

    内容: 1.问题引入 2.暴力求解方法 3.优化方法 4.KMP算法 1.问题引入 原始问题: 对于一个字符串 str (长度为N)和另一个字符串 match (长度为M),如果 match 是 st ...

  6. ansible的安装与使用

    ansible的特点: 1. 基于ssh运行 2. 无需客户端 安装ansible 这里提供四种安装方式,根据自己的需要任选一种即可 1.1使用yum安装 yum install epel-relea ...

  7. sqlserver查询---分配cpu等资源

    数据库资源按需分配 https://www.cnblogs.com/i6first/p/4138365.html https://blog.csdn.net/kk185800961/article/d ...

  8. HTML5 通过文件输入框读取文件为base64文件, 并借助canvas压缩 FileReader, files, drawImage

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. Java 跨域 CrossOrigin注解 Filter拦截 Nginx配置

    说明 资源请求的发起方与请求的资源不在同一个域中的: 一般的,只要网站的[协议名protocol].[主机host].[端口号port]这三个中的任意一个不同,网站间的数据请求与传输便构成了跨域调用: ...

  10. 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值

    使用XMLHttpRequest对象分为4部完成: 1.创建XMLHttpRequest组建 2.设置回调函数 3.初始化XMLHttpRequest组建 4.发送请求 实例代码: [javascri ...