【LeetCode】174. Dungeon Game
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.
Credits:
Special thanks to @stellari for adding this problem and creating all test cases.
由于最终目标是骑士到达公主位置,因此在右下角必须满足HP剩余1.
从右下角位置开始倒推,每个位置需要同时满足两个条件:(1)该位置HP为1(保证不死),(2)该位置的HP足够到达公主(使用动态规划)
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
if(dungeon.empty() || dungeon[].empty())
return ;
int m = dungeon.size();
int n = dungeon[].size();
vector<vector<int> > minHP(m, vector<int>(n,));
for(int i = m-; i >= ; i --)
{
for(int j = n-; j >= ; j --)
{
if(i == m- && j == n-)
minHP[i][j] = max(, -dungeon[i][j]);
else if(i == m-)
minHP[i][j] = max(, minHP[i][j+]-dungeon[i][j]);
else if(j == n-)
minHP[i][j] = max(, minHP[i+][j]-dungeon[i][j]);
else
minHP[i][j] = max(, min(minHP[i+][j]-dungeon[i][j], minHP[i][j+]-dungeon[i][j]));
}
}
return minHP[][];
}
};
【LeetCode】174. Dungeon Game的更多相关文章
- 【LeetCode】174. Dungeon Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- java 利用HttpURLConnection方式获取restful格式的服务数据
/** * @Author: * @Description:利用HttpURLConnection方式获取restful格式的服务数据 * @Date: */ private static List& ...
- JAVA的Split小技巧
在日常的开发中截取字符串必不可少,但是在JAVA中的Split截取有点特点的地方是 例如: String str=1,2,3,; 那么 str.split(&q ...
- Hadoop:安装ftp over hdfs
https://blog.csdn.net/sptoor/article/details/11484855 https://blog.csdn.net/tengxing007/article/deta ...
- (转)NGUI中深度depth和z轴关系
先列出转载链接: http://game.ceeger.com/forum/read.php?tid=8917 转载原文: 问题源自一个帖子,因为上传的图比较多,就另开了这个贴写下自己的试验结果,原帖 ...
- Jquery的分页插件
Jquery的分页插件, 用起来还不错. 来自: http://flaviusmatis.github.io/simplePagination.js/ 下载地址: https://github.c ...
- Idea卡在Maven导入工程
Idea卡在Maven导入工程的解决办法 1.看日志 tail -100f cd ~/Library/Logs/IntelliJIdea2017.2/idea.log 你可以换下路径中Idea的版本或 ...
- [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...
- 【python】bytes与字符串的相互转化
代码: # bytes转字符串方式一 b=b'\xe9\x80\x86\xe7\x81\xab' string=str(b,'utf-8') print(string) # bytes转字符串方式二 ...
- 关于Puppet不得不说的故事
Puppet对于做DevOps的同学来说,是个熟悉的名字,但仍有许多人并不了解它.那么我先来简单介绍一下:Puppet是由Puppetlabs公司开发的系统管理框架和工具集,被用于IT服务的自动化管理 ...
- LintCode: Happy Number
C++ class Solution { public: /** * @param n an integer * @return true if this is a happy number or f ...