[LeetCode] 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) |
Note:
- 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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
这道王子救公主的题还是蛮新颖的,我最开始的想法是比较右边和下边的数字的大小,去大的那个,但是这个算法对某些情况不成立,比如下面的情况:
1 (K) | -3 | 3 |
0 | -2 | 0 |
-3 | -3 | -3 (P) |
如果按我的那种算法走的路径为 1 -> 0 -> -2 -> 0 -> -3, 这样的话骑士的起始血量要为5,而正确的路径应为 1 -> -3 -> 3 -> 0 -> -3, 这样骑士的骑士血量只需为3。无奈只好上网看大神的解法,发现统一都是用动态规划 Dynamic Programming 来做,建立一个二维数组 dp,其中 dp[i][j] 用来表示当前位置 (i, j) 出发的起始血量,最先处理的是公主所在的房间的起始生命值,然后慢慢向第一个房间扩散,不断的得到各个位置的最优的生命值。逆向推正是本题的精髓所在啊,仔细想想也是,如果从起始位置开始遍历,我们并不知道初始时应该初始化的血量,但是到达公主房间后,我们知道血量至少不能小于1,如果公主房间还需要掉血的话,那么掉血后剩1才能保证起始位置的血量最小。那么下面来推导状态转移方程,首先考虑每个位置的血量是由什么决定的,骑士会挂主要是因为去了下一个房间时,掉血量大于本身的血值,而能去的房间只有右边和下边,所以当前位置的血量是由右边和下边房间的可生存血量决定的,进一步来说,应该是由较小的可生存血量决定的,因为较我们需要起始血量尽可能的少,因为我们是逆着往回推,骑士逆向进入房间后 PK 后所剩的血量就是骑士正向进入房间时 pk 前的起始血量。所以用当前房间的右边和下边房间中骑士的较小血量减去当前房间的数字,如果是负数或着0,说明当前房间是正数,这样骑士进入当前房间后的生命值是1就行了,因为不会减血。而如果差是正数的话,当前房间的血量可能是正数也可能是负数,但是骑士进入当前房间后的生命值就一定要是这个差值。所以我们的状态转移方程是 dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j])。为了更好的处理边界情况,我们的二维 dp 数组比原数组的行数列数均多1个,先都初始化为整型数最大值 INT_MAX,由于我们知道到达公主房间后,骑士火拼完的血量至少为1,那么此时公主房间的右边和下边房间里的数字我们就都设置为1,这样到达公主房间的生存血量就是1减去公主房间的数字和1相比较,取较大值,就没有问题了,代码如下:
解法一:
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size(), n = dungeon[].size();
vector<vector<int>> dp(m + , vector<int>(n + , INT_MAX));
dp[m][n - ] = ; dp[m - ][n] = ;
for (int i = m - ; i >= ; --i) {
for (int j = n - ; j >= ; --j) {
dp[i][j] = max(, min(dp[i + ][j], dp[i][j + ]) - dungeon[i][j]);
}
}
return dp[][];
}
};
我们可以对空间进行优化,使用一个一维的 dp 数组,并且不停的覆盖原有的值,参见代码如下:
解法二:
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size(), n = dungeon[].size();
vector<int> dp(n + , INT_MAX);
dp[n - ] = ;
for (int i = m - ; i >= ; --i) {
for (int j = n - ; j >= ; --j) {
dp[j] = max(, min(dp[j], dp[j + ]) - dungeon[i][j]);
}
}
return dp[];
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/174
类似题目:
参考资料:
https://leetcode.com/problems/dungeon-game/
https://leetcode.com/problems/dungeon-game/discuss/52774/C++-DP-solution
https://leetcode.com/problems/dungeon-game/discuss/52843/6-lines-16-ms-C++-O(mn)-Time-O(n)-Space
https://leetcode.com/problems/dungeon-game/discuss/52790/My-AC-Java-Version-Suggestions-are-welcome
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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]174. Dungeon Game地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- LeetCode Dungeon Game
原题链接在这里:https://leetcode.com/problems/dungeon-game/ 这是一道DP题,保存当前格到右下格所需要的最小体力,m*n的dp数组保存. 更新是Math.mi ...
- [LeetCode] Zuma Game 祖玛游戏
Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...
- 174 Dungeon Game 地下城游戏
一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格布局.我们英勇的骑士(K)最初被安置在左上角的房间里,并且必须通过地下城对抗来拯救公主.骑士具有以正整数 ...
- leetcode-174. Dungeon Game 地下城游戏
一道关于骑士救公主故事的题目. 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过 ...
- [LeetCode] Elimination Game 淘汰游戏
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...
- [LeetCode] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
随机推荐
- 通过Http接口及SolrNet 两种方法基于Solr5.5.1 实现CURD
前言 老规矩,任何技术的入门我通常都会总结增删改查,本文我就通过HttpWebRequest和SolrNet的方式实现Solr最基础的增删改查(CURD).对于自己的完整项目,同时不想过于依赖第三方类 ...
- 重定向Http status code 303 和 302
http 302 http 303 Http 302 302是一个普通的重定向代码.直观的看来是,请求者(浏览器或者模拟http请求)发起一个请求,然后服务端重定向到另一个地址.而事实上,服务端仅仅是 ...
- ASP.NET Core 中文文档 第二章 指南(4.10)检查自动生成的Detail方法和Delete方法
原文 Examining the Details and Delete methods 作者 Rick Anderson 翻译 谢炀(Kiler) 校对 许登洋(Seay).姚阿勇(Mr.Yao) 打 ...
- CSS知识总结(四)
CSS常用样式 2.元素样式 1)宽度 width:auto|length 单位:设置以像素计的宽度值(px) 设置以百分比计的宽度值(%) 例:p {width:200px;} div {width ...
- 【中文分词】隐马尔可夫模型HMM
Nianwen Xue在<Chinese Word Segmentation as Character Tagging>中将中文分词视作为序列标注问题(sequence labeling ...
- [AngularJS] AngularJS系列(3) 中级篇之表单验证
目录 基本验证 验证插件messages 自定义验证 基本验证 <form name="form" novalidate ng-app> <span>{{f ...
- C# listview 单击列头实现排序 <二>
单击列头实现排序,首先在羡慕中添加下面的帮助实现的类:具体的代码: using System; using System.Collections; using System.Windows.Forms ...
- PHP如何实现网址伪静态
Apache的 mod_rewrite是比较强大的,在进行网站建设时,可以通过这个模块来实现伪静态. 主要步骤如下: 1.检测Apache是否开启mod_rewrite功能 可以通过php提供 ...
- (学习笔记)laravel 中间件
(学习笔记)laravel 中间件 laravel的请求在进入逻辑处理之前会通过http中间件进行处理. 也就是说http请求的逻辑是这样的: 建立中间件 首先,通过Artisan命令建立一个中间件. ...
- activiti工作流的web流程设计器整合视频教程 SSM和独立部署
本视频为activiti工作流的web流程设计器整合视频教程 整合Acitiviti在线流程设计器(Activiti-Modeler 5.21.0 官方流程设计器) 本视频共讲了两种整合方式 1. 流 ...