https://leetcode.com/problems/dungeon-game/

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:

1.The knight's health has no upper bound.

2. 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.

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

leetcode@ [174] Dungeon Game (Dynamic Programming)的更多相关文章

  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 地牢游戏 --------- java

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

  3. Java for LeetCode 174 Dungeon Game

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

  4. Leetcode#174 Dungeon Game

    原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...

  5. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  6. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  7. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  8. [leetcode]174. Dungeon Game地牢游戏

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

  9. LeetCode 174. Dungeon Game (C++)

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

随机推荐

  1. uva10943

    递推  还是比较容易的 /************************************************************************* > Author: ...

  2. 网站404,500错误页面的处理,及500异常写入errorLog日志

    1.web.xml 配置 <error-page> <error-code>404</error-code> <location>/404.jsp< ...

  3. tomcat 设置默认编码格式

    在tomcat目录下 conf文件夹下的server.xml中: <Connector port="80" protocol="HTTP/1.1"     ...

  4. hbase集群 常用维护命令

    一. zk集群 1. 查看当前服务的角色 leader/follower echo stat|nc 127.0.0.1 2181 2.  启动ZK服务: sh bin/zkServer.sh star ...

  5. highcharts 柱形堆叠图

    <!doctype html> <html lang="en"> <head> <script type="text/javas ...

  6. javaweb学习总结(四十七)——监听器(Listener)在开发中的应用

    监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...

  7. Java API ——Object类

    1.Object类概述 1)类层次结构的根类.       2)所有类都直接或者间接的继承自该类. 3)构造方法            · public Object()            · 子 ...

  8. poj 1416 Shredding Company( dfs )

    我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...

  9. sencha项目升级

    对于已经开发好的sencha项目进行升级,要做的有以下几步(以sencha2.2.0升级到sencha2.3.1为例): 1,下载Sencha-2.3.1sdk,下载地址:http://cdn.sen ...

  10. 各种好用的工具之一 ---- PNGGauntlet

    1.PNGGauntlet实际上是一个图形前端,压缩图像的过程中使用的是PNGOUT, OptiPNG, 和DeflOpt这三款软件. 软件官网:http://pnggauntlet.com/ 可自行 ...