leetcode 174. 地下城游戏 解题报告

leetcode 174. 地下城游戏

一些恶魔抓住了公主(P)并将她关在了地下城的右下角。地下城是由 M x N 个房间组成的二维网格。我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主。

骑士的初始健康点数为一个正整数。如果他的健康点数在某一时刻降至 0 或以下,他会立即死亡。

有些房间由恶魔守卫,因此骑士在进入这些房间时会失去健康点数(若房间里的值为负整数,则表示骑士将损失健康点数);其他房间要么是空的(房间里的值为 0),要么包含增加骑士健康点数的魔法球(若房间里的值为正整数,则表示骑士将增加健康点数)。

为了尽快到达公主,骑士决定每次只向右或向下移动一步。

-2 (K) -3 3
-5 -10 1
10 30 -5 (P)

编写一个函数来计算确保骑士能够拯救到公主所需的最低初始健康点数。

例如,考虑到如下布局的地下城,如果骑士遵循最佳路径 右 -> 右 -> 下 -> 下,则骑士的初始健康点数至少为 7。

说明:

骑士的健康点数没有上限。

任何房间都可能对骑士的健康点数造成威胁,也可能增加骑士的健康点数,包括骑士进入的左上角房间以及公主被监禁的右下角房间。

分析

根据题目的描述,这道题目是动态规划无疑。但是如何建立递推关系表达式呢?我们令dp[i][j]表示从(i,j)出发到公主所在位置所需的最小的初始血量。那么dp[0][0]+1即为救出公主所需的最小初始健康值。因为骑士的健康值在任何时候都不能低于1,所以此处加了1。下面我们分析下地推关系。我们知道骑士可以往下走或者往右走。如果\(dungeon[i][j] \ge dp[i][j+1]\),那么由\((i,j)\rightarrow (i,j+1)\),由\(dungeon[i][j]\)提供的魔法球增加的健康值足够骑士走到公主所在位置,不需要补给额外的能量。否则,则需要提供额外的健康值\(dp[i][j+1] - dungeon[i][j]\)才足以维持骑士的生命。由\((i,j)\rightarrow (i+1,j)\)也是同理,于是我们有如下的表达

\[
dp[i][j] = max(0, min(dp[i+1][j], dp[i][j+1])-dungeon[i][j])
\]

上式可以保证在任何时候\(dp[i][j]\ge 0\)。

下面给出代码

class Solution {
public:
    int calculateMinimumHP(vector<vector<int>>& dungeon) {
        const int m = dungeon.size();
        const int n = dungeon[0].size();
        int dp[m][n] = {};
        dp[m-1][n-1] = dungeon[m-1][n-1] >= 0?0:-dungeon[m-1][n-1];
        for(int i = m-1; i >= 0; --i) {
            for(int j = n-1; j >= 0; --j) {
                int ret = 0x3f3f3f3f;
                if(i < m - 1)
                    ret = min(ret, dp[i+1][j] - dungeon[i][j]);
                if(j < n - 1)
                    ret = min(ret, dp[i][j+1] - dungeon[i][j]);
                if (i < m - 1 or j < n - 1)
                    dp[i][j] = max(0, ret);
            }
        }
        return dp[0][0] + 1;
    }
};

原文地址:https://www.cnblogs.com/crackpotisback/p/10185383.html

时间: 2024-10-08 08:17:43

leetcode 174. 地下城游戏 解题报告的相关文章

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[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 example i

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

【LeetCode】3Sum Closest 解题报告

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from