[leetcode]174. Dungeon Game地牢游戏
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地牢游戏的更多相关文章
- [LeetCode] 174. Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- 174 Dungeon Game 地下城游戏
一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...
- Java for LeetCode 174 Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Leetcode#174 Dungeon Game
原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...
- LeetCode 174. Dungeon Game (C++)
题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...
- leetcode 174. 地下城游戏 解题报告
leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...
随机推荐
- matlab下将图片序列转化为视频文件 && 将为视频文件转化图片序列
将图片序列转化为视频文件 程序如下: framesPath = 'E:\img\';%图像序列所在路径,同时要保证图像大小相同 videoName = 'Bolt.avi';%表示将要创建的视频文件的 ...
- labview如何生成可执行文件
labview生成可执行文件可以分为两种情况. 第一种,是电脑中有labview软件开发环境的情况 第二种,是电脑中没有安装labview软件开发环境 下面是一个简单的labview代码: 程序解释: ...
- 最简单的TCP、UDP案例及各函数的详细解释
TCP: server #include "stdafx.h" #include<iostream> #define BUF_SZIE 64 #include &quo ...
- 批处理-通过mono把c#编译成dll
::copyright@cjy @echo off ::mcs.exe address set addrMcs=D:\Program Files\Unity\Editor\Data\MonoBleed ...
- BI开发(ETL-DW)
来到公司已经参与开发了一段时间的BI项目,但是仅仅是按照需求开发,今天下午公司给大家培训数据仓库的知识,老大(女程序员)在上面讲,我们在下面听,2到3个小时吧,什么纬度,主题,几乎听的一脸茫然,最后演 ...
- java编译器知识
代码编译器: 代码: 编译就是讲一种代码编译成计算机可以理解的指令. ================================================================= ...
- 给iOS开发新手送点福利,简述UIDatePicker的用法
1.Locale 设置DatePicker的地区,即设置DatePicker显示的语言. 1.跟踪所有可用的地区,取出想要的地区 NSLog(@"%@", [NSLocale av ...
- Spark standalone运行模式
Spark Standalone 部署配置 Standalone架构 手工启动一个Spark集群 https://spark.apache.org/docs/latest/spark-standalo ...
- sencha touch 小米3无法点击问题 修复
修改源码文件夹下event/publisher/Dom.js中的attachListener方法,代码如下 attachListener: function(eventName, doc) { if ...
- 关于VS+ImageWatch在线调试问题
1.使用VS肯定离不开在线调试 2.使用Opencv在VS下进行图像处理,那肯定少不了Image Watch 这两个软件在线调试都存在大坑,弄得精疲力尽才找到解决办法!!! 以下问题都可以通过这个设置 ...