【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 ...
随机推荐
- Android传感器应用——重力传感器实现滚动的弹球
一. 问题描述 Android中有多达11种传感器,不同的手机设备支持的传感器类型也不尽相同 1. 重力传感器 GV-sensor 2. 加速度传感器 G-sensor 3. 磁力传感器 M-se ...
- mysql开启日志sql语句
#查看日期情况 #show variables like '%general%'; #开启日志 #SET GLOBAL general_log = 'On'; #指定日志文件 #SET GLOBAL ...
- 【Spark】SparkStreaming-提交到集群运行
SparkStreaming-提交到集群运行 spark streaming 提交_百度搜索 SparkStreaming示例在集群中运行 - CSDN博客
- SQL多表连接查询(详细实例)(转)
http://www.xker.com/page/e2012/0708/117368.html select * from student,course where student.ID=course ...
- memcache的资料集
1. 安装与查看过程(完整) https://www.cyberciti.biz/faq/rhel-fedora-linux-install-memcached-caching-system-rpm/ ...
- AOP在大规模软件开发项目中的应用(图)
AOP在大规模软件开发项目中的应用(图) 本文的写作源于一个真实的大型软件开发项目,我们努力尝试在这个项目中推广应用AOP.在此我们将对曾经面临过的一些实际问题与困难进行分析,试图引发关于面向方面软件 ...
- Solr搭建真实项目
在经过solr5.4.1 quick start 的学习后.我们要搭建真实的项目了 1.启动项目 bin/solr start 2.创建core bin/solr create -c pms 3.为 ...
- ShopEx customSchema 定制能够依据客户的需求对站点进行对应功能的加入改动或者删除
站内锚文本制作 1.改动config.php,在文件末尾添加下面内容 define('CUSTOM_CORE_DIR',BASE_DIR . '/custom'); 2.添加custom目录(与cor ...
- ArcGIS10.6了解一下
因为计算机水平不断更新,ESRI不得不重新倾力打造下一代ArcMap,叫ArcGIS Pro,现在ArcGIS Pro功能有一定地突显,但还不够强大和稳定:而ArcGIS Desktop方面没有什么大 ...
- Mysql数据库常用分库和分表方式
http://blog.csdn.net/clevercode/article/details/50877580 1 分库 1.1 按照功能分库 按照功能进行分库.常见的分成6大库: 1 用户类 ...