【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 ...
随机推荐
- Nginx+Tomcat实现反向代理与动静分离
1. 什么是动静分离 所谓动静分离就是通过nginx(或apache等)来处理用户端请求的静态页面,tomcat(或weblogic)处理动态页面,从而达到动静页面访问时通过不同的容器来处理. 2. ...
- Cocos2d-x数据持久化-查询数据
数据查询一般会带有查询条件,这可以使用SQL语句的where子句实现,但是在程序中需要动态绑定参数给where子句.查询数据的具体操作步骤如下所示.(1) 使用sqlite3_open函数打开数据库. ...
- iOS夯实:内存管理
iOS夯实:内存管理 文章转自 内存管理 最近的学习计划是将iOS的机制原理好好重新打磨学习一下,总结和加入自己的思考. 有不正确的地方,多多指正. 目录: 基本信息 旧时代的细节 新时代 基本信息 ...
- 抓取dump
1,在程序奔溃前部署.adplus.exe -crash -pn explorer.exe -o d: -crash:当进程挂掉的时候抓取dump,只能抓取到进程报错时的信息,如果进程不报错,就无法抓 ...
- CCM加密学习
这几天终于搞定了AES硬件加密工具的使用,几种简单的加密模式也都实验通过了,比较麻烦的一种是CCM模式的加密,它是CTR加密模式和CMAC认证算法的混合使用.本文先介绍CCM模式的原理与基本实现,然后 ...
- [转]Nuget挂了的解决方法
今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...
- [转]PHP5.5安装PHPRedis扩展
phpredis是个人觉得最好的一个php-redis客户端,因为其提供的function与redis的命令基本一致,降低的了学习成本,同时功能也很全面. 一.linux安装方法 phpredis下载 ...
- 从对偶问题到KKT条件
转自:http://xuehy.github.io/%E4%BC%98%E5%8C%96/2014/04/13/KKT/ 从对偶问题到KKT条件 Apr 13, 2014 对偶问题(Duality) ...
- Call与Apply
1.前言 ECMAscript中提供了两个方法(call,apply)用于改变对象内部的this指针,它们两个的作用都是一样的,但是传递的参数有点不大相同. 它们的大概语法为: call(this, ...
- 【C#高级编程(学习与理解)】1.1 C#与.NET的关系
1.C#语言不能孤立使用,而必须和.NET Framework一起考虑.C#编译器专门用于.NET,这表示用C#编写的所有代码总是在.NET Framework中运行. 2.C#就其本身而言只是一种语 ...