LeetCode之“动态规划”: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.

  Credits:
  Special thanks to @stellari for adding this problem and creating all test cases.

  这道题跟以前做过的不一样的是,动态规划的方向是从右下角到左上角。程序如下:

 1 class Solution {
 2 public:
 3     int calculateMinimumHP(vector<vector<int>>& dungeon) {
 4         int rows = dungeon.size();
 5         if(rows == 0)
 6             return 0;
 7         int cols = dungeon[0].size();
 8
 9         vector<vector<int> > dp(rows, vector<int>(cols, INT_MIN));
10
11         dp[rows-1][cols-1] = max(1, 1 - dungeon[rows-1][cols-1]);
12         for(int i = rows - 2; i > -1; i--)
13             dp[i][cols-1] = max(1, dp[i+1][cols-1] - dungeon[i][cols-1]);
14         for(int j = cols - 2; j > -1; j--)
15             dp[rows-1][j] = max(1, dp[rows-1][j+1] - dungeon[rows-1][j]);
16
17         for(int i = rows - 2; i > -1; i--)
18             for(int j = cols - 2; j > -1; j--)
19                 dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j]);
20
21         return dp[0][0];
22     }
23 };
时间: 2024-10-07 05:30:23

LeetCode之“动态规划”:Dungeon Game的相关文章

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

【Leetcode】动态规划问题详解(持续更新)

1.动态规划算法步骤(Dynamic Programming) 动态规划算法一般用来求解最优化问题,当问题有很多可行解,而题目要求寻找这些解当中的"最大值"/"最小值"时,通常可以采用DP. 动态规划算法与分治法相似,都是通过组合子问题的解来求解原问题.所不同的是,动态规划应用于子问题重叠的情况,在递归求解子问题的时候,一些子子问题可能是相同的,这种情况下,分治法会反复地计算同样的子问题,而动态规划对于相同的子问题只计算一次. 动态规划算法的设计步骤: 1.刻画最优

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

LeetCode之“动态规划”:Unique Binary Search Trees &amp;&amp; Unique Binary Search Trees II

1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题目分析参考自一博

LeetCode之“动态规划”:Longest Valid Parentheses

题目链接 题目要求: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another e

LeetCode之动态规划

62. Unique Paths QuestionEditorial Solution Total Accepted: 86710 Total Submissions: 239084 Difficulty: Medium A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or rig

LeetCode之“动态规划”:Maximum Subarray

题目链接 题目要求: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If yo

[LeetCode] HouseRobber 动态规划

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will autom

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi