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下显示ip所属位置

    在linux下,要是网络出现延迟,通常我们需要分析自己到对端的服务器的网络环境 例:ping www.baidu.com traceroute www.baidu.com 通过分析来确定大概是什么问题 ...

  2. iOS版本控制git小结--yoowei

    # 显示隐藏文件 defaults write com.apple.finder AppleShowAllFiles Yes && killall Finder # 不显示隐藏文件 d ...

  3. Orchard源码分析(6):Shell相关

    概述在Orchard中,提出子站点(Tenant)的概念,目的是为了增加站点密度,即一个应用程序域可以有多个子站点. Shell是子站点(Tenant)级的单例,换句话说Shell代表了子站点.对比来 ...

  4. Xcode常用技巧(1)-使用Xcode进行代码分析及GDB调试

    1. 使用Xcode分析代码,分析分为静态分析及动态分析 静态分析:(Xcode-Product-Analyze)  检测代码是否有潜在的内存泄露  编译器认为不太合适的代码 运行结果: 若程序有 ...

  5. Hadoop伪分布式搭建(一)

     下面内容主要说明在Windows虚拟机上面,怎么搭建一个Hadoop伪分布式,并如何运行wordcount程序和网页查看HDFS文件系统. 1 相关软件下载和安装 APACH官网提供hadoop版本 ...

  6. LINUX下搭建VPN

    一.准备 需要 dkms-2.0.17.5-1.noarch.rpm.ppp-2.4.5-33.0.rhel6.x86_64.rpm.pptpd-1.4.0-1.el6.x86_64.rpm,并依次安 ...

  7. Java实现zip压缩多个文件下载

    为了更好的演示,首先创建一个文件实体FileBean,包含了文件路径和文件名称: package com.javaweb.entity; import java.io.Serializable; /* ...

  8. A funny story in regard to a linux newbie

    ZZ from here :  ask what kernel ring buffer is A few days ago I started thinking that my linux educa ...

  9. parastor2000挂载方式

    1.先授权 2.客户端安装使用0528parastor-client-centos6.6-38390.tar.xz clusconf -f fatnodes --sync-do "cd /m ...

  10. 关于EXCEL学习的那些事

    由于在客服中心工作,虽然日常工作基本与数据打交道,但是周围的同事对EXCEL基本不怎么了解,仅会一些基本操作.所以基本日常我会被问到许多EXCEL相关的问题,也针对这个做了一些整理与思考. 之后无意看 ...