LeetCode 174. Dungeon Game (C++)
题目:
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) |
Note:
- 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.
分析:
给定一个二维数组,一个骑士从左上角开始,只能向下或者向右走,最终要走到右下角救公主。每经过一个格子,要减去或加上相应的生命值,骑士要活着达到右下角,求骑士初始生命值最低为多少。
我们可以维护一个二维数组用来表示骑士在当前格子也就是dp[ i ][ j ]需要的最少生命值。先来看一个特殊的情况,因为到达最后一个格子,加上dungeon[ m ][ n ]后要有1生命值。如果dungeon[ m ][ n ]是负数,骑士到达该位置时要扣去相应的生命值,且最少要1生命,如果dungeon[ m ][ n ]是正数,则只需要1生命就够了,因为到这个位置还可以加生命值,所以不难发现dp[ m ][ n ] = max(1 - dungeon[ m ][ n ],1)。下面再来看通常情况,dp[ i ][ j ]的值实际上是由dp[ i+1 ][ j ]和dp[ i ][ j+1 ]来决定的,也就是骑士右面和下面哪个需要的生命值越少,则骑士会选择那条较少的路线。所以动态转移方程dp[ i ][ j ] = max(min(dp[ i+1 ][ j ],dp[ i ][ j+1 ]) - dungeon[ m ][ n ],1)。
为了方便计算我们可以扩充一行一列,便于计算边界值。
-2 (K) | -3 | 3 |
-5 | -10 | 1 |
10 | 30 | -5 (P) |
7 | 5 | 2 | INT_MAX |
6 | 11 | 5 | INT_MAX |
1 | 1 | 6 | 1 |
INT_MAX | INT_MAX | 1 | INT_MAX |
程序:
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size();
int n = dungeon[].size();
vector<vector<int>> res(m+, vector<int>(n+,INT_MAX));
res[m][n-] = res[m-][n] = ;
for(int i = m-; i >= ; --i){
for(int j = n-; j >= ; --j){
res[i][j] = max(min(res[i+][j], res[i][j+])-dungeon[i][j], );
}
}
return res[][];
}
};
LeetCode 174. Dungeon Game (C++)的更多相关文章
- [LeetCode] 174. Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
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#174 Dungeon Game
原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...
- [leetcode]174. Dungeon Game地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- leetcode 174. 地下城游戏 解题报告
leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...
- 【LeetCode】174. Dungeon Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】174. Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
随机推荐
- 中国城市json
[{ "label": "北京Beijing010", "name": "北京", "pinyin" ...
- Spring整合MyBatis(一)MyBatis独立使用
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. MyBatis本是Apache的一个开源项目iBatis,2010年这 ...
- Handlebars模板引擎
介绍 Handlebars 是 JavaScript 一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less template"(无逻辑模 ...
- ZOJ3202-Second-price Auction(根据输入输出判断是队列还是栈)
A Stack or A Queue? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
- UVa 10377 - Maze Traversal
題目:一個機器人在迷宮中行走,它的指令是方向控制(前進.左轉.右轉).給你初始位置和一些指令: 問最後停在那個位置. 分析:模擬.直接模擬就可以,注意一下細節. 假设,不能行走(邊界或者是墻壁)則停在 ...
- pipeline 发布war包
pipline 写法分为 脚本式和声明式,下面采用脚本式编程: node { stage('checkout') { echo '开始检出代码' checkout([$class: 'GitSCM', ...
- #leetcode刷题之路50-Pow(x, n)
实现 pow(x, n) ,即计算 x 的 n 次幂函数.示例 1:输入: 2.00000, 10输出: 1024.00000示例 2:输入: 2.10000, 3输出: 9.26100 #inclu ...
- json_decode($str,true)的结果为null
//$result为传进来的json值 $result = $this->params['auth_result']; //html_entity_decode进行HTML 实体转换为字符 // ...
- Apache 流框架 Flink,Spark Streaming,Storm对比分析(二)
本文由 网易云发布. 本文内容接上一篇Apache 流框架 Flink,Spark Streaming,Storm对比分析(一) 2.Spark Streaming架构及特性分析 2.1 基本架构 ...
- SQLAlchemy Table(表)类方式 - Table类和Column类
Table 构造方法 Table(name, metadata[, *column_list][, **kwargs]) 参数说明: name 表名 metadata 元数据对象 column_lis ...