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. Android Http请求框架一:Get 和 Post 请求

    1.HttpUtil package com.app.android01; import java.io.BufferedReader; import java.io.IOException; imp ...

  2. PAT 01-1

    #include <stdio.h> int main() { int i; int k; ]; scanf("%d", &k); ; i < k; i+ ...

  3. clang: error: no such file or directory: xxx.pch

    今天打开一个下载的例子 报clang: error: no such file or directory: xxx.pch的错 说一下解决方案 1.先在你的工程里找到这.pch文件- 2.把它现在的路 ...

  4. OOD沉思录 --- 类和对象的关系 --- 包含关系2

    4.6 尽量让类中定义的每个方法尽可能多地使用包含的对象(即数据成员) 这其实就是高内聚的翻版强调.如果每个类的情况并非如此,那很可能是这一个类表示了两个或更多的概念,记住一个类只应该表示一个概念. ...

  5. 【Android】 Android实现录音、播音、录制视频功能

    智能手机操作系统IOS与Android平分天下(PS:WP与其他的直接无视了),而Android的免费招来了一大堆厂商分分向Android示好,故Android可能会有“较好”的前景. Android ...

  6. 读书笔记——Windows核心编程(8)Interlocked系列函数

    先让我们来复习下小学知识 A+B=C//式中A为被加数,B为加数. A-B=C//式中A为被减数,B为减数. 再让我们来明确一个知识点:返回值为void的Windows函数意味着一定会执行成功. -- ...

  7. jQuery Validate 表单验证插件----自定义校验结果样式

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  8. Post-office 树状数组

    Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered from 1 to N for si ...

  9. Legends-ggplot2图例的一些操作

    移除图例 require(ggplot2) b = qplot(Sepal.Length,Petal.Length,data=iris,geom="point",colour = ...

  10. 学完STM32开发板,就选4412开发板让你有目标的学习嵌入式开发

    600余页用户使用手册 linux实验手册(资料不断更新)100期配套零基础高清视频教程 轻松入门 (资料不断更新)2000人售后认证群 在线支持 售后无忧 源码全开源  原厂技术资料经典学习书籍推荐 ...