【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 ...
随机推荐
- Js 中的this
关于this This代表"当前"对象 谁调用就代表谁 行内绑定 不带参数 行内绑定 带参数 this 当前对象 Object HTMLInputElement 动态绑定 要获取自 ...
- Java使用wkhtmltox实现HTML代码生成PDF文档或者图片
由于项目需要,把HTML代码转为PDF或者图片进行保存.最开始使用Flying Saucer来把HTML代码生成为PDF文档,功能已经开发出来了,也能够完成转换功能,期间也遇到了中文支持以及图片路径的 ...
- 定制的Server-Sent Events 聊天服务器
//匿名聊天服务器 //将新的消息POST到/chat地址,或者以GET形式从通一个URL获取文本或事件流 //创建一个GET请求到"/"来返回一个简单的HTML文件,这个文件包括 ...
- Linux一
1,debian默认需要手动开启SSH连接# Authentication:LoginGraceTime 120PermitRootLogin without-passwordStrictModes ...
- Article Master Data Deviation
Site data – Logistics DC / Logistics Store Where is the reference site decided when you maintain the ...
- mysql安装启动教程(两种方法)
mysql安装启动: 方法一(简单版): cmd进入mysql安装的bin目录:mysqld.exe –install net start mysql 服务启动(或者选择计算机->(右键)管理 ...
- DB Cache Reloaded Fix缓存不能被激活解决方法
1.创建wp-content/plugins/db-cache-reloaded-fix/cache目录. 2.将cache权限改为777. 3.拷贝wp-content/plugins/db-cac ...
- JS 正则 获取URL参数
function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- SQL技巧之行列转换
比如:id 姓名 状态 1 刘德华 12 刘德华 23 周华健 04 吴彦祖 1 在access中,用一条sql查询 ...
- IE=edge,chrome=1的META信息详解
这几天在玩 HTML5 ★ Boilerplate,注意到meta信息中有这么一句: 复制代码 代码如下: <meta http-equiv="X-UA-Compatible" ...