题目:

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++)的更多相关文章

  1. [LeetCode] 174. Dungeon Game 地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  2. leetcode@ [174] Dungeon Game (Dynamic Programming)

    https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...

  3. ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  4. Java for LeetCode 174 Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  5. Leetcode#174 Dungeon Game

    原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...

  6. [leetcode]174. Dungeon Game地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  7. leetcode 174. 地下城游戏 解题报告

    leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...

  8. 【LeetCode】174. Dungeon Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  9. 【LeetCode】174. Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...

随机推荐

  1. windows 下nginx配置php支持

    修改nginx配置 location ~ \.php$ { root D:/Learn/php/test/; fastcgi_pass ; fastcgi_index index.php; fastc ...

  2. python logging模块按天滚动简单程序

    简单日志按天滚动,加入apsheduler,用crontab模式按小时运行测试: import logging from logging.handlers import TimedRotatingFi ...

  3. 2-51单片机ESP8266学习-AT指令(开发板51单片机自动冷启动下载原理)

    前言:了解就行,不必深究 上一篇链接 http://www.cnblogs.com/yangfengwu/p/8720148.html 源码链接:https://pan.baidu.com/s/1wT ...

  4. ios学习路线—Objective-C(Runtime消息机制)

    RunTime简称运行时.就是系统在运行的时候的一些机制,其中最主要的是消息机制.对于C语言,函数的调用在编译的时候会决定调用哪个函数( C语言的函数调用请看这里 ).编译完成之后直接顺序执行,无任何 ...

  5. BUAA OO 2019 第一单元作业总结

    目录 总 架构 Controller​ Model​ 输入处理 代码静态分析 行数 方法复杂度 UML​ 类图 优点 缺点 坑 输入 非法的空白字符 输入的简并处理 运算 浅拷贝 可变类型与不可变类型 ...

  6. R语言数据结构二

    上节我们讲到R语言中的基本数据类型,包括数值型,复数型,字符型,逻辑型以及对应的操作和不同数值类型之间的转换.众所周知,R语言的优势在于进行数据挖掘,大数据处理等方面,因此单个的数据并不能满足我们的需 ...

  7. JavaWeb基础—会话管理之Cookie

    一.什么是会话 打开浏览器,浏览各种资源,点击各种超链接,直至关闭浏览器,整个过程称为会话 二.会话管理的两种技术 1.Cookie 基于客户端.在客户端记录信息来确认用户身份.以cookie的形式写 ...

  8. IPC学习

    课下作业-IPC 要求: 研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接 共享内存 管道 FIFO 信号 消息队列 共享内存 共享内存可以说是最有用的进程间通信方 ...

  9. 01- ajax, 登录验证,json数据,文件上传

    1.ajax简介 1.向服务器发送请求的途径 # 向服务器发送请求的途径 1. 浏览器地址栏,默认get请求 2. form表单: get请求 post请求 3. a标签,默认get请求 4. Aja ...

  10. error: this 'if' clause does not guard... [-Werror=misleading-indentation]

    解决办法就是if语句的下面加{} 报错的 if (!pMem) return LOS_NOK; 修改后 if (!pMem) { return LOS_NOK; }