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. ...
随机推荐
- 加载外部JavaScript的最佳方法
当<script>标记是一个HTML文档流,浏览器必须停止渲染并等待脚本文件下载并执行,然后再继续(例子).通过JavaScript创建一个新的<script>标签可以避免这个 ...
- 静态页分页功能js代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- log4j - 配置文件
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息. 通过配置,可以创建出Log4J的运行环 ...
- ps插件安装
CutAndSliceMe.zxp 切图插件安装,下载后改为zip后缀,再解压后 复制文件夹到(PS软件安装目录)PhotoshopCC\Plug-ins\Panels文件夹下面
- Sublime之旅
安装 http://www.sublimetext.com/3 常用操作 window版本 CTRL + P 打开文件搜索 Ctrl+K+B 打开目录树 Ctrl+Shift+[ ...
- PHP中有关Session的函数比较多,最常用到的也就这么几个函数
php中的cookie与session技术详解 一.cookie介绍 cookie常用于识别用户.cookie是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送coo ...
- 微博java SDK介绍及使用说明
转自:作者:新浪微博 开放平台 @MUNTO_AKIRA http://open.weibo.com/blog/%E5%BE%AE%E5%8D%9Ajava-sdk%E4%BB%8B%E7%BB%8D ...
- HDU 1069 Monkey and Banana(二维偏序LIS的应用)
---恢复内容开始--- Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- backtrack5渗透 笔记
目录 1.信息收集 2.扫描工具 3.漏洞发现 4.社会工程学工具 5.运用层攻击msf 6.局域网攻击 ...