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. ubuntu14.0安装arm-linux-gcc交叉编译环境

    1.下载文件: 安装包:arm-linux-gcc-4.5.1-v6-vfp-20120301.tgz 下载地址  http://pan.baidu.com/s/1pJwQ6Sj 2.开始安装(建议大 ...

  2. Android 购物车功能的实现

    首先,众所周知,ListView是Android最常用的控件,可以说是最简单的控件,也可以说是最复杂的控件. 作为一个Android初级开发者,可能会简单的ListView展示图文信息. 作为一个有一 ...

  3. 应用程序代理AppDelegate解析

    应用程序UIApplication是通过代理和外部交互的,当应用程序生命周期中发生改变时UIApplication就会调用代理对应的方法. @implementation AppDelegate - ...

  4. 错误:StrictMode $ AndroidBlockGuardPolicy.onNetwork

    you have to insert 2 lines "StrictMode" on MainActivity Class, example's below: 在onCreate( ...

  5. 博客建议(Suggestions)

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

  6. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  7. android基础开发之scrollview

    scrollView 是android系统提供的一种 特殊的展示view. 其实我们很早就遇到过scrollview的东东,比如listview. 而google官方文档也提出,不要混合使用scrol ...

  8. 2013MPD上海6.23 PM 光耀:读心术,用户心理的产品之道

    创新的前提是:制度与组织的创新!!!!!!!!!!!!!! 光耀:腾讯互联网业务产品经理(腾讯公司互联网业务系统产品经理.在电子商务.社会化媒体等方面有深入研究.参与腾讯多个重要项目产品工作) 什么是 ...

  9. Effective Java 78 Consider serialization proxies instead of serialized instances

    Serialization proxy pattern private static nested class (has a single constructor whose parameter ty ...

  10. 未能找到元数据文件“引用的DLL的路径”

    使用VS的时候   偶尔会出现错误 [未能找到元数据文件“引用的DLL的路径”] 但是实际上项目中这些DLL都是做了引用的,甚至你前一天打开还是好好的,睡一觉起来 不知道什么原因 就酱紫了 原因:不详 ...