【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 ...
随机推荐
- Memcached学习(一)
1.Memcached是什么? 引用维基百科上得简介,Memcached 是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,目前已被诸如Facebook等许多 ...
- Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现
HelloWorld场景就是游戏中的主菜单场景.HelloWorld.h文件代码如下: #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h& ...
- WiFi相关知识
1. 无线路由器的覆盖范围和带机量 覆盖范围取决于发射功率.接收灵敏度,以及对端的发射功率和接收灵敏度.因为Wi-Fi设备在发出数据包后,一定要等待对方回ACK才会确认发送成功,所以,如果任意一方的发 ...
- Windows内存管理[转]
本文主要内容:1.基本概念:物理内存.虚拟内存:物理地址.虚拟地址.逻辑地址:页目录,页表2.Windows内存管理3.CPU段式内存管理4.CPU页式内存管理 一.基本概念1. 两个内存概念物理内存 ...
- [ZZ+CH] Html5 canvas+js 时钟
总之新Blog入驻以后,又开始老习惯,到处折腾自定义的空间,放些东西. 想起以前大一的时候做过一个Javascript的时间显示器,现在想做一个时钟,当然现在老奸巨猾,会先去看一看有前辈写过没. 前辈 ...
- Ubuntu、Sql Server卸载心得
这几天真是搞得亏大了! 首先是卸载Ubuntu,直接在Windows下格式化那个盘了,这就出岔子了……然后越来越糟糕,最后弄得一个系统都没有了……然后重装系统…… 然后装VS和Sql Server,因 ...
- 谈谈Nullable<T>的类型转换问题
本篇文章讨论可空值类型(Nullable<T>)的转换,却确地说是如何将一种类型的值对象转换成相应的可空值.这来源于今天我们的一个成员遇到的一个小问题,我经过一些整理写了这篇文章.虽然没有 ...
- Unity学习笔记(1):认识Unity
Unity是什么? Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 它提供了创建(或者装配)对象实例的机制,而这些对象实例可能 ...
- javascript dom追加内容的例子
javascript dom追加内容的使用还是比较广泛的,在本文将为大家介绍下具体的使用方法. 例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- discuz!X2.5技术文档
discuz!系统常量: DISCUZ_ROOT //网站根目录 TIMESTAMP //程序执行的时间戳 CHARSET //程序的编码类型 FORMHASH //HASH值 其余 ...