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. sqlmap写文件为空之谜

    恰逢有一个SQL注入可以通过sqlmap进行,而且权限高得离谱,直接就是root权限.既然是root权限当然是想直接getshell咯.可是只是sqlmap -u xxx --os-shell的时候却 ...

  2. mysql dumpfile与outfile函数的区别

    一直以为两个函数作用是相同的   经过简单测试发现还是有些区别的   如下表admin   mysql> select * from admin;   +-----+-----------+-- ...

  3. js动态生成表格

    动态生成表格           *创建一个页面:两个输入框和一个按钮 *代码和步骤                      /*                          1.得到输入的行 ...

  4. Java并发编程核心方法与框架-TheadPoolExecutor的使用

    类ThreadPoolExecutor最常使用的构造方法是 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAli ...

  5. java调用存储过程

    在做java调用sqlserver存储过程时遇到了各种各样的问题,不过在不懈的努力之下这些问题还是得以解决了.今天总结一下遇到的问题以及解决的方法. 首先调用存储过程的方法大家都很清楚: String ...

  6. MEF搜索范围

    MEF对扩展组件的查找范围通常有三个: AssemblyCatalog:从某个程序集中查找. ApplicationCatalog:在应用程序所在的目录下查找. DirectoryCatalog:在某 ...

  7. c# 字符串前加@

    @在c#中为强制不转义的符号,在里面的转义字符无效. 例如:Console.WriteLine("你好\t吗?"); Console.WriteLine(@"你好\t吗& ...

  8. [译]Profile and debug your ASP.NET MVC app with Glimpse

    原文:http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse Gl ...

  9. [译]View components and Inject in ASP.NET MVC 6

    原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...

  10. Eclipse 自动补全功能失效解决办法及修改快捷键方法

    最近在学习Java,前段时间分盘把电脑能坏了,重装系统后发现我的Eclipse的自动补全的功能失效了,那多麻烦呀,什么都得自己打,于是百度后总结了以下解决方法: 1.点击Window-->Pre ...