题目:

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. PAT乙级1014

    1014 福尔摩斯的约会 (20 分)   大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d& ...

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

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

  3. iOS渐变导航栏封装

    由于最近开发的几个项目都有渐变导航栏,每次写的时候都要copy一堆关于导航渐变相关的代码,显得类很冗余,所以花了点时间封装了一个渐变类,直接继承就可以满足大部分需求啦,这里简单写一下心路历程: 渐变的 ...

  4. 面向对象之static关键字

    static概念 static它是静态修饰符,一般用来修饰类中的成员. static特点 1.多个对象共享一个static成员变量.一个对象将static成员变量值修改了,其他对象中的static成员 ...

  5. C++ 容器类型成员

    类型别名   iterator 此容器类型的迭代类型 const_iterator 可以读取元素,但不能修改元素的迭代器类型 size_type 无符号整数类型,足够保存此种容器类型最大可能容器的大小 ...

  6. JavaWeb基础—MySQL入门小结

    一.数据库概述 RDBMS:关系型数据库管理系统 == 管理员(manager)+仓库(database) 常见数据库:  Oracle(神喻):甲骨文 MySQL: 归于甲骨文旗下(高版本系统已经开 ...

  7. uliweb的模版

    uliweb模版的文件名是与函数名相同的 以test为例: ***@Android:~/ablog# vim apps/blog/templates/test.html 编辑test.html的内容 ...

  8. 20145202马超 2016-2017-2 《Java程序设计》第9周学习总结

    20145202马超 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交 ...

  9. 20155307刘浩《网络对抗》逆向及Bof基础

    20155307刘浩<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函数,foo函数会回显任何用户输 ...

  10. 20155327 李百乾 Exp4 恶意代码分析

    20155327 李百乾 Exp4 恶意代码分析 基础问题回答 (1)如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所以想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法 ...