LeetCode Dungeon Game
原题链接在这里:https://leetcode.com/problems/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) |
Notes:
- The knight's health has no upper bound.
- Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
题解:
DP, 需要保存当前格到右下格所需要的最小体力.
递归时, 是Math.min(走右侧最小体力,左下侧最小体力).
先出示右下角的点,再初始最后一行和最后一列.
Note: 1.最后返回的不是dp[0][0], 而是dp[0][0]加一,因为之前求得体力值的最小值是0, 但骑士的体力值必须是正数.
2. 之所以选择从后往前更新而不是从前往后更新是因为,从前往后更新时求得的局部最优不保证是全局最优。
AC Java:
class Solution {
public int calculateMinimumHP(int[][] dungeon) {
if(dungeon == null || dungeon.length == 0 || dungeon[0].length == 0){
return 0;
} int m = dungeon.length;
int n = dungeon[0].length; int [][] dp = new int[m][n];
dp[m-1][n-1] = dungeon[m-1][n-1] < 0 ? -dungeon[m-1][n-1]:0;
for(int i = m-2; i>=0; i--){
dp[i][n-1] = dp[i+1][n-1] - dungeon[i][n-1] > 0 ? dp[i+1][n-1] - dungeon[i][n-1] : 0;
}
for(int j = n-2; j>=0; j--){
dp[m-1][j] = dp[m-1][j+1] - dungeon[m-1][j] > 0 ? dp[m-1][j+1] - dungeon[m-1][j] : 0;
} for(int i = m-2; i>=0; i--){
for(int j = n-2; j>=0; j--){
int cost = Math.min(dp[i+1][j], dp[i][j+1]);
dp[i][j] = cost - dungeon[i][j] > 0 ? cost - dungeon[i][j] : 0;
}
}
return dp[0][0]+1;
}
}
LeetCode Dungeon Game的更多相关文章
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- 【leetcode dp】Dungeon Game
https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- ✡ 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
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- Java for LeetCode 174 Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
随机推荐
- MySQL 记录不存在时插入 记录存在则更新的实现方法
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; INSERT 中 ON DUPLICATE KEY UP ...
- Pointcut is not well-formed: expecting 'identifier' at character position 0
异常如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDa ...
- Android Studio 想说爱你不容易
开始使用Android Studio 真是非常痛苦的一段经历,而这一切的根源就在于GFW,俗称“墙” 如果避过墙来安装 AS,其实我已经在另外一篇文章中说明:http://www.cnblogs.co ...
- YII2.0 secruity
保存密码不能用明文保存,用MD5或者sha1哈希化是安全,但是随着硬件的发展,可能会暴力破解,目前能够对抗暴力破解的哈希算法是 bcrypt,Yii提供了两个帮助函数使用crypt进行安全的哈希加密 ...
- iPads和iPhones的Media Queries
iPad Media Queries 1.iPad Media Queries (所有版本,包括iPad mini) iPads从第一代到至今,总共有五代,也就是iPad1~iPad5,以及Mini ...
- Antialiasing with Transparency
Antialiasing with Transparency This sample demonstrates the GeForce 7 Series per-primitive super-sam ...
- CentOs 6.6 安装配置 SVN
① 挂载光盘 mount /dev/cdrom /mnt/cdrom ② yum 安装 svn yum -y install subversion ③ 创建svn 版本库根目录 mkdir -p /w ...
- How to Write a Spelling Corrector
http://norvig.com/spell-correct.html Feb 2007to August 2016 How to Write a Spelling Corrector One we ...
- Flink - Generating Timestamps / Watermarks
https://ci.apache.org/projects/flink/flink-docs-release-1.0/apis/streaming/event_timestamps_watermar ...
- Java 特殊性领会
1. 字符串比较绝对不能用 == 而必须是 xx.equals() 2. 多有对象new 后都是以引用的方式存在着 3. 数组list, map 类型都不能边用for 循环边删除,迭代器可以 eg: ...