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. HTML字符实体(Character Entities),转义字符串(Escape Sequence)

    为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,>,用于链接签,&用于转义),不能直接使用.这些符号是不显示在我们最终看到的网页里的,那如果我们希 ...

  2. HeadFirst设计模式之门面模式

    一. 1.The Facade Pattern provides a unifi ed interface to a set of interfaces in a subsytem. Facade d ...

  3. 第一个C语言代码

    #include<stdio.h> void main() {     int g1,g2,g3,r1,r2,r3,n;     int m=0;     float ave;     i ...

  4. POJ2965——The Pilots Brothers' refrigerator

    The Pilots Brothers' refrigerator Description The game “The Pilots Brothers: following the stripy el ...

  5. Android安全问题 抢先拦截短信

    同上篇文章一样,这里只陈述结果,代码分析稍后给出 导读:本文叙述如何先于某些伪杀毒软件.病毒.常规软件获取到短信 众所周知,android系统在收到短信息的时候会发送广播,但是此广播是有序广播,也就是 ...

  6. cmd启动tomcat

    1.安装jdk 2.安装tomcat 3.需要配置两个用户环境变量,仅仅配置系统变量没用. a)JAVA_HOME:D:\programing~tools\java~tools\JDK(tm)\jdk ...

  7. javascript 简单的计算器

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  8. codevs3732 解方程

    %%%.设f(x)=a0+a1x+a2x^2+ - + anx^n.求f(x)=0的x. 数据范围很大,高精度只能骗分. 运用类似hash的思想. 如果这个等式mod p 还成立(p为质数)那它很可能 ...

  9. 百度分享不支持https的解决方案

    站点自从开启 https 之后 ,百度分享就不能用了!但是又寻找不到类似百度分享的替代品.. 怎么办呢?要如何解决 百度分享不支持https的问题呢, 跟着博主动动手,让你百度分享仍然能在https下 ...

  10. I.MX6 Android iperf3 porting failed

    /***************************************************************************** * I.MX6 Android iperf ...