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.

思路:

题目虽然长,但是很明显是标准的动态规划。从右下角向前反向推算即可。minimumHP[i][j]存储到达[i][j]位置时需要的最少血量。

class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
if(dungeon.empty())
return ;
vector<vector<int>> minimumHP(dungeon.size(), vector<int>(dungeon[].size()));
minimumHP.back().back() = (dungeon.back().back() >= ) ? : - dungeon.back().back();
for(int i = dungeon.size() - ; i >= ; i--) //最右边一列
{
minimumHP[i].back() = max(, minimumHP[i + ].back() - dungeon[i].back());
}
for(int i = dungeon[].size() - ; i >= ; i--) //最下边一行
{
minimumHP.back()[i] = max(, minimumHP.back()[i + ] - dungeon.back()[i]);
}
for(int i = dungeon.size() - ; i >= ; i--)
{
for(int j = dungeon[].size() - ; j >= ; j--)
{
minimumHP[i][j] = min(max(, minimumHP[i][j + ] - dungeon[i][j]), max(, minimumHP[i + ][j] - dungeon[i][j]));
}
}
return minimumHP[][];
}
};

【leetcode】Dungeon Game (middle)的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  5. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  6. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

随机推荐

  1. ORA-00001: unique constraint (...) violated解决方案

    ORA-00001: unique constraint (...) violated 的解决方案 今天往Oracle数据库里插入数据一条记录的时候,报错了, 控制台抛出异常:违反唯一性约定, 我以为 ...

  2. 创建Mysql 序列

    create table sequence( name ) not null primary key, current_value , increment , max_value BIGINT, -- ...

  3. The influence of informal governance mechanisms on knowledge integration

    Title:The influence of informal governance mechanisms on knowledge integration within cross-function ...

  4. 《JS高级程序设计》笔记 —— 解析查询字符串

    今天在继续翻阅<JS高级程序设计>的时候,正好翻到location对象这一小节,其中有一部分就是讲的解析查询字符串.看到这个内容立马想到了做去哪儿秋招笔试题的时候有这么一道题. 去哪儿笔试 ...

  5. Tomcat遇到的问题The Tomcat server configuration at ServersTomcat v5.5 Server at localhost-config is missing. Check..

    一.解决方法 删除Servers视图,重新创建一个即可.

  6. Bootstrap学习笔记(三) 网格系统

    4-1实现原理 网格系统的实现原理非常简单,仅仅是通过定义容器大小,平分12份(也有平分成24份或32份,但12份是最常见的),再调整内外边距,最后结合媒体查询,就制作出了强大的响应式网格系统.Boo ...

  7. Redirect and POST in ASP.NET

    http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET

  8. 多种的android进度条的特效源码

    多种的android进度条的特效源码,这个源码是在源码天堂那个网站上转载过来的,我已经修改一部分了,感觉很实用的,大家可以学习一下吧,我就不上传源码了,大家可以直接到那个网站上下载吧. 源码天堂下载地 ...

  9. Mysql 数据库安装配置

    MySQL的多种安装方法 在当今的互联网企业,Mysql数据服务几乎都是运行在LINUX系统操作系统上,当然你也可以在WINDOWS.UNIX等商业操作系统上运行. 但是一般企业都会采用LNMP.LA ...

  10. Web开发常见的漏洞

    SQL注入漏洞 跨站脚本攻击漏洞 登陆后台管理页面 IIS短文件/文件夹漏洞 系统敏感信息泄露