【leetcode】Dungeon Game (middle)
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.
思路:
题目虽然长,但是很明显是标准的动态规划。从右下角向前反向推算即可。minimumHP[i][j]存储到达[i][j]位置时需要的最少血量。
class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
if(dungeon.empty())
return ;
vector<vector<int>> minimumHP(dungeon.size(), vector<int>(dungeon[].size()));
minimumHP.back().back() = (dungeon.back().back() >= ) ? : - dungeon.back().back();
for(int i = dungeon.size() - ; i >= ; i--) //最右边一列
{
minimumHP[i].back() = max(, minimumHP[i + ].back() - dungeon[i].back());
}
for(int i = dungeon[].size() - ; i >= ; i--) //最下边一行
{
minimumHP.back()[i] = max(, minimumHP.back()[i + ] - dungeon.back()[i]);
}
for(int i = dungeon.size() - ; i >= ; i--)
{
for(int j = dungeon[].size() - ; j >= ; j--)
{
minimumHP[i][j] = min(max(, minimumHP[i][j + ] - dungeon[i][j]), max(, minimumHP[i + ][j] - dungeon[i][j]));
}
}
return minimumHP[][];
}
};
【leetcode】Dungeon Game (middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
随机推荐
- Eclipse 调试maven test
在eclipse中调试maven test 一般情况下,使用如下方式都不能使myeclipse检测到程序中的断点: 项目 -> Run As -> maven test 或 项目 -> ...
- jQuery实现CheckBox全选、全不选
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- (转)IDG副总裁楼军:顶级VC青睐什么样的创业者
学习能力是创业者的第一能力 创业者首先要有格局观和很强的学习能力. 具体什么意思?比如说去年IDG投了一个做C2C平台的海淘项目,创始人之前其实是帮他爱人做海淘代购.他爱人是一个代购买手,赚得还不错, ...
- emacs_1
--> 正在处理依赖关系 perl(VMS::Filespec),它被软件包 perl-PathTools-3.2701-1.el5.rf.x86_64 需要---> 软件包 perl-p ...
- iOS 非ARC基本内存管理系列 2-多对象内存管理(3) 利用@property来自动管理内存
iOS 基本内存管理-多对象内存管理(2)中可以看到涉及到对象的引用都要手动管理内存:每个对象都需要写如下代码 // 1.对要传入的"新车"对象car和目前Person类对象所拥有 ...
- ubuntu后台配置无线网络
一.静态配置: 1.编辑 /etc/network/interfaces: auto loiface lo inet loopback auto wlan0iface wlan0 inet stati ...
- 屏蔽ubuntu桌面鼠标右键以及Ctrl Alt F*
1.屏蔽右键: xmodmap -e "pointer = 1 2 99"xmodmap -e 'pointer = 1 2 0 4 5 6 7 8 9' #xmodmap -e ...
- 译文:Javascript-Functions
个人理解+google翻译+有道翻译.如有错误,请指正.原文来自MDN:Functions Functions是javascript基本构建模块之一.每一个function是一个javascript程 ...
- jquery返回顶部特效
<style> p#back-to-top{position:fixed; bottom:100px;right:10px; display: none; } p#back-to-top ...
- Responsive设计——不同设备的分辨率写法
1.1024px显屏 @media screen and (max-width : 1024px) { /* 样式写在这里 */ } 2.800px显屏 @media screen and (max- ...