Dungeon Game -- latched
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.
基本思路:
动态规划
设health[i][j] 为走进dungeon[i][j]的初始血量,且该个血量将能维持到骑士足以走完右下角。
已知条件:骑士走完右下角至少要剩一滴血。即health[m][n-1] = 1。
m为dungeon行数。
也能够设health[m-1][n] = 1。
此值表示走完右下角的剩余血量。
同一时候也是从该右下角向右,或者向下。走到还有一格时的初始血量。 当然此两格是虚拟的,地牢中不存在。或者形象的说,从右下角向右或者向下走出地牢后,剩余的血量。
从此点。能够倒推出health[0][0]。
骑士仅仅能向右,向下右移动。 要知道当前位置的初始血量,仅仅须要知道其右和其下的初始血量,就能够反推出。
即
health[i][j] = min(health[i+1][j], health[i[j+1]) - dungeon[i][j]
因为骑士要时刻保持血量至少为1. 上面能够改为:
health[i][j] = max(1, min(health[i+1][j], health[i[j+1]) - dungeon[i][j])
因为递推时仅仅须要其右和其下,两个位置, 能够使用滚动数组。用一维替换掉二维数组。
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
if (dungeon.empty() || dungeon[0].empty())
return 0;
const int m = dungeon.size();
const int n = dungeon[0].size();
vector<int> health(n+1, INT_MAX);
health[n-1] = 1;
for (int i=m-1; i>=0; i--) {
for (int j=n-1; j>=0; j--) {
health[j] = max(1, min(health[j], health[j+1]) - dungeon[i][j]);
}
}
return health[0];
}
};
Dungeon Game -- latched的更多相关文章
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- leetcode174. Dungeon Game
// learn from https://discuss.leetcode.com/topic/6912/c-dp-solution ''' class Solution { public: int ...
- 【leetcode】Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- Dungeon Game ——动态规划
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Java for LeetCode 174 Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- LeetCode Dungeon Game
原题链接在这里:https://leetcode.com/problems/dungeon-game/ 这是一道DP题,保存当前格到右下格所需要的最小体力,m*n的dp数组保存. 更新是Math.mi ...
随机推荐
- 读《实战GUI自动化测试》之:第三步,如何提高测试结果分析的效率
转自:http://www.ibm.com/developerworks/cn/rational/r-cn-guiautotesting3/ 所谓自动化测试,就是“自动化”+“测试”.自动化本身显然不 ...
- 联想 P70-t 免解锁BL 免rec Magisk Xposed 救砖 ROOT
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- Android基础TOP5_5:设置没有标题栏而且用系统壁纸当背景的界面
在res/values目录下的style.xml设置如下 <style name="AppBaseTheme" parent="android:Theme.Wall ...
- oracle dos命令
1.无账户密码登录数据库:sqlplus/nolog 后面不能加分号,否则不能识别 2.登录数据库:sqlplus 3.在sql下测试连接性:conn oracle_name/oracle_passw ...
- SQL函数-汉字首字母查询
汉字首字母查询处理用户定义函数 CREATE FUNCTION f_GetPY1(@str nvarchar(4000))RETURNS nvarchar(4000)ASBEGIN DECLARE @ ...
- 3星|林毅夫《战胜命运》:事实证明华盛顿共识是错误的,GIFF是穷国发展正道。
本书是林毅夫与喀麦隆一位经济学家合著.基本的观点是:事实证明华盛顿共识是错误的,GIFF是穷国发展正道.GIFF的主要思想是政府找到对标国家,强力推行产业政策. 作为一个经济学外行,读后感觉关于华盛顿 ...
- 三维卷积:全景图像Spherical CNNs(Code)
卷积神经网络(CNN)可以很好的处理二维平面图像的问题.然而,对球面图像进行处理需求日益增加.例如,对无人机.机器人.自动驾驶汽车.分子回归问题.全球天气和气候模型的全方位视觉处理问题. 将球形信号的 ...
- RabbitMQ系列(二)--基础组件
声明:对于RabbitMQ的学习基于某课网相关视频和<RabbitMQ实战指南>一书,后续关于RabbitMQ的博客都是基于二者 一.什么是RabbitMQ RabbitMQ是开源代理和队 ...
- mybatis 使用参数
Parameter 1. 传入简单类型 JAVA代码: public User get(Long id) { return (User) getSqlSession().selectOne(&q ...
- python json格式和csv文件转换
python json格式和csv文件转换 上代码 import csv import json ''' json格式示例 [{ "firstName":"Bill&qu ...