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

运用动态规划的思想,在每个节点,其HP最小为1。从P处往回推导,每个位置HP最小为1,又必须保证能到达P处。

 class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
if(dungeon.empty()||dungeon[].empty())
return ;
int m,n;
m=dungeon.size();
n=dungeon[].size();
vector<vector<int>> minHP(m,vector<int>(n,));
for(int i=m-;i>=;i--)
{
for(int j=n-;j>=;j--)
{
if(i==m-&&j==n-)
minHP[i][j]=max(,-dungeon[i][j]);
else if(i==m-)
{
minHP[i][j]=max(,(minHP[i][j+]-dungeon[i][j]));
}
else if(j==n-)
minHP[i][j]=max(,(minHP[i+][j]-dungeon[i][j]));
else
minHP[i][j]=max(,min(minHP[i+][j]-dungeon[i][j],minHP[i][j+]-dungeon[i][j]));
}
}
return minHP[][];
}
};

Dungeon Game ——动态规划的更多相关文章

  1. 174. Dungeon Game(动态规划)

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

  2. LeetCode之“动态规划”:Dungeon Game

    题目链接 题目要求: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of ...

  3. 动态规划——Dungeon Game

    这又是个题干很搞笑的题目:恶魔把公主囚禁在魔宫的右下角,骑士从魔宫的左上角开始穿越整个魔宫到右下角拯救公主,为了以最快速度拯救公主,骑士每次只能向下或者向右移动一个房间, 每个房间内都有一个整数值,负 ...

  4. [LeetCode] Dungeon Game 地牢游戏

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

  5. 【leetcode】Dungeon Game

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

  6. 【leetcode】Dungeon Game (middle)

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

  7. 动态规划Dynamic Programming

    动态规划Dynamic Programming code教你做人:DP其实不算是一种算法,而是一种思想/思路,分阶段决策的思路 理解动态规划: 递归与动态规划的联系与区别 -> 记忆化搜索 -& ...

  8. 【LeetCode】174. Dungeon Game

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

  9. Dungeon Game (GRAPH - DP)

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

随机推荐

  1. 适配iPhone6和iPhone6 Plus

    先对比所有市面上的iPhone设备,然后分析如何适配新的设备,   iPhone4,iPhone4s 分辨率960*640  长宽比1.5iPhone5,iPhone5s  分辨率1136*640   ...

  2. NSMutable sort排序

    Compare method Either you implement a compare-method for your object: - (NSComparisonResult)compare: ...

  3. 《android传感器高级编程》译者序

    翻看手机中的应用,就能发现大多数应用都已经使用了传感器.让微信彻底火起来的“附近的人”和“摇一摇”.碰一碰交换信息的Bump.各种运动记录app.神奇的“磁力探测仪”.火爆的游戏Temple Run… ...

  4. Stooge排序与Bogo排序算法

    本文地址:http://www.cnblogs.com/archimedes/p/stooge-bogo-sort-algorithm.html,转载请注明源地址. Stooge排序算法 Stooge ...

  5. iOS项目上传到AppStore步骤流程

    1.登录developer.apple.com 2.点击member center后 进下图 3.点击certificates Identifiers进下图 4.点击Certificates进下图,首 ...

  6. Mac搭建本地svn服务器,并用Cornerstone连接服务器

    Mac默认已经安装了svn,我们只需要进行配置并开启就可以了 首先我们可以验证一下是否安装了svn,打开终端,输入命令 svnserve --version 这里可以看到目前svn的版本号,说明已经安 ...

  7. 博客建议(Suggestions)

    I don't know if you will like the music. But I am sure there are some songs which are really wonderf ...

  8. Android开发艺术探索学习笔记(三)

    第三章  View的事件体系 3.1 View基础知识 3.1.1 什么是view View 是Android中所有控件的基类,是一种界面层的控件的一种抽象,它代表了一个控件. 3.1.2 View的 ...

  9. 长文件名导致的0x80070057

    今天遇到件怪事. 把一个视频集(86G)从电脑硬盘转移动硬盘的时候里面时报里面的两个文件夹里的视频和字幕不能复制 错误代码0x80070057 这个视频集是从校内PT是下下来的,电脑是联想Y560-w ...

  10. PS网页设计教程XXVI——如何在PS中创建一个专业的网页布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...