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:

  • 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.
 
 
从右下角开始进行动态规划
设有m行,n列
 
考虑边界条件:
最底部的元素:
若dungeon[m-1][n-1]>=0,则
dp[m-1][n-1]=0;
若dungeon[m-1][n-1]<0,则
dp[m-1][n-1]=-dungeon[m-1][n-1];
可以简化为dp[m-1][n-1]=max(-dungeon[m-1][n-1],0);
 
最右边一列
若dungeon[m-2][n-1]>=0,则
dp[m-2][n-1]=max(dp[m-1][n-1]-dungeon[m-2][n-1],0);
若dungeon[m-2][n-1]<0,则
dp[m-2][n-1]=dp[m-1][n-1]+abs(dungeon[m-2][n-1]);
                      =dp[m-1][n-1]-dungeon[m-2][n-1];
 
 
化简为dp[m-2][n-1]=max(dp[m-1][n-1]-dungeon[m-2][n-1],0);
 
 
同理最下边一行
dp[m-1][n-2]=dp[m-1][n-1]-dungeon[m-1][n-2];
 
 
对于其他位置的元素
dp[i][j]=max(min(dp[i+1][j],dp[i][j+1])-dungeon,0)
 
 
 class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) { int m=dungeon.size();
int n=dungeon[].size();
vector<vector<int>> dp(m,vector<int>(n)); dp[m-][n-]=max(-dungeon[m-][n-],); for(int i=m-;i>=;i--)
{
dp[i][n-]=max(dp[i+][n-]-dungeon[i][n-],);
} for(int j=n-;j>=;j--)
{
dp[m-][j]=max(dp[m-][j+]-dungeon[m-][j],);
} for(int i=m-;i>=;i--)
{
for(int j=n-;j>=;j--)
{ dp[i][j]=max(min(dp[i+][j],dp[i][j+])-dungeon[i][j],);
}
} return dp[][]+;
}
};

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

  1. 【leetcode】Dungeon Game (middle)

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

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. Linux的io机制

    Linux的io机制 Buffered-IO 和Direct-IO Linux磁盘I/O分为Buffered IO和Direct IO,这两者有何区别呢? 对于Buffered IO: 当应用程序尝试 ...

  2. 把字符转换为 HTML 实体

    把字符转换为HTML实体:htmlentities() 把HTML实体转换回字符:html_entity_decode() 把预定义的字符 "<" (小于)和 "& ...

  3. PHP与Javascript的混合测试

    js调用php <?php $num=88; ?> <script> var a = <?php echo $num;?>; alert(a); </scri ...

  4. gvim如何显示html属性代码提示? vim 如何显示 javascript属性及方法提示?

    gvim如何显示html属性代码 可以在vim中 显示 html, css, js等的属性/方法 提示: 一是: 在 ~/.vim/after/syntax/ 目录中 安装 css-color.vim ...

  5. JAVA语言学习笔记(一)

    1 一切都是对象 JAVA中所有代码都必须写在类里面. 方法名和参数列表(它们合起来被称为"方法签名")唯一地标识出某个方法.联想多态. 基本数据类型的"局部变量&quo ...

  6. 简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下   本文实例讲述了简单实用的PHP防注 ...

  7. webpack入门(一)——webpack 介绍

    如今的网站正在演化为web应用程序: 1. 越来越多的使用JavaScript. 2. 现代浏览器提供更广泛的接口. 3. 整页刷新的情况越来越少,甚至更多代码在同一个页面.(SPA) 因此有很多代码 ...

  8. 常见的几个angular.js的问题

    来源于网络收集 一.ng-show/ng-hide 与 ng-if的区别? 我们都知道ng-show/ng-hide实际上是通过display来进行隐藏和显示的.而ng-if实际上控制dom节点的增删 ...

  9. select2

    .select2-container .select2-choice { height: 34px; line-height: 34px; } .自定义 组件高度 在css 里面设置 .select2 ...

  10. ACM3 求最值

    /*2*2014.11.18*求最值*描述:给定N个整数(1<=N<=100),求出这N个数中的最大值,最小值.*输入:多组数据,第一行为一个整数N,第二行为N个不超过100的正整数,用空 ...