【leetcode】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.
class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int m=dungeon.size();
int n=dungeon[].size();
vector<vector<int>> dp(m,vector<int>(n));
dp[m-][n-]=max(-dungeon[m-][n-],);
for(int i=m-;i>=;i--)
{
dp[i][n-]=max(dp[i+][n-]-dungeon[i][n-],);
}
for(int j=n-;j>=;j--)
{
dp[m-][j]=max(dp[m-][j+]-dungeon[m-][j],);
}
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[][]+;
}
};
【leetcode】Dungeon Game的更多相关文章
- 【leetcode】Dungeon Game (middle)
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 【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 ...
随机推荐
- Yii2框架与MongoDB拓展、Redis拓展的安装流程
@author 周煦辰 2016-03-21 这段时间新上了一个项目,使用的是Yii2框架.这里记录一下Yii2框架.Yii2-Mongo拓展.Yii2-Redis拓展等的安装流程.因为使用的系统是W ...
- aop实现日志(转)
文章来至于http://www.thinksaas.cn/group/topic/341436/ 第一:>>在spring的配置文件里增加以下配置 <!-- 支持 @AspectJ ...
- 自定义layout中需要重写的方法
Layout类中,有3个方法是必定会被依次调用: prepareLayout: 准备所有view的layoutAttribute信息 collectionViewContentSize: 计算cont ...
- C# 连接mongodb副本集+分片读写分离及学习资料
一.副本集配置 搭建完毕,1台主实例.1台从实例.1台仲裁实例.mongodb建议副本集中的机器数量为奇数,即至少需要3台实例 二.副本集连接字符串 1.读 mongodb://secondary.c ...
- mysql 导入sql文件,source命令
转自:http://blog.sina.com.cn/s/blog_610997850100mwv8.html 今天碰到个问题要用phpmyadmin导入1G的数据,但是在怎么都导入不了,用命令行就可 ...
- vtk第一个程序
首先 设置环境变量 解决方案资源管理器 选中项目名字 而不是ALL_BUILD 选项卡 项目-属性 右侧 附加依赖项->编辑 kernel32.libuser32.libgdi32.libwin ...
- Sublime Text3
Sublime Text3 激活码: ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E- 813A03DD 5E4AD9E ...
- 阿里云9折推荐码:0LGVW2
阿里云9折推荐码:0LGVW2,第一次购买云服务器或云数据库可享受原价9折优惠.
- [译]Profile and debug your ASP.NET MVC app with Glimpse
原文:http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse Gl ...
- ELK日志分析系统搭建(转)
摘要: 前段时间研究的Log4j+Kafka中,有人建议把Kafka收集到的日志存放于ES(ElasticSearch,一款基于Apache Lucene的开源分布式搜索引擎)中便于查找和分析,在研究 ...