LeetCode //C - 174. Dungeon Game

174. Dungeon Game

The demons had captured the princess 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 was initially positioned in the top-left room and must fight his way through 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 (represented by negative integers), so the knight loses health upon entering these rooms; other rooms are either empty (represented as 0) or contain magic orbs that increase the knight's health (represented by positive integers).

To reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.

Return the knight's minimum initial health so that he can rescue the princess.

Note that 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.

Example 1:

Input: dungeon = \[-2,-3,3,-5,-10,1,10,30,-5]
Output: 7
Explanation: The initial health of the knight must be at least 7 if he follows the optimal path: RIGHT-> RIGHT -> DOWN -> DOWN.

Example 2:

Input: dungeon = \[0]
Output: 1

Constraints:
  • m == dungeon.length
  • n == dungeoni.length
  • 1 <= m, n <= 200
  • -1000 <= dungeonij <= 1000

From: LeetCode

Link: 174. Dungeon Game


Solution:

Ideas:

1. Dynamic Programming Table (DP Table):

  • Create a 2D array dp where dpij stores the minimum initial health required to reach the princess starting from cell (i, j).

2. Base Case Initialization:

  • For the princess's cell (m-1, n-1), set dpm-1n-1 to the maximum of 1 and 1 - dungeonm-1n-1.

3. Filling the DP Table:

  • Last Column: Calculate minimum health for each cell in the last column based on the cell below it.
  • Last Row: Calculate minimum health for each cell in the last row based on the cell to the right.
  • Other Cells: For each cell (i, j), compute the minimum health based on the minimum of the right and below cells, adjusted by the current cell's value. If the resulting health is less than or equal to 0, set it to 1.

4. Result:

  • The value in dp00 represents the minimum initial health required for the knight to rescue the princess starting from the top-left corner.
Code:
c 复制代码
int calculateMinimumHP(int** dungeon, int dungeonSize, int* dungeonColSize) {
    int m = dungeonSize;
    int n = dungeonColSize[0];
    int dp[m][n];
    
    dp[m-1][n-1] = dungeon[m-1][n-1] > 0 ? 1 : 1 - dungeon[m-1][n-1];
    
    for (int i = m - 2; i >= 0; i--) {
        dp[i][n-1] = dp[i+1][n-1] - dungeon[i][n-1];
        if (dp[i][n-1] <= 0) dp[i][n-1] = 1;
    }
    
    for (int j = n - 2; j >= 0; j--) {
        dp[m-1][j] = dp[m-1][j+1] - dungeon[m-1][j];
        if (dp[m-1][j] <= 0) dp[m-1][j] = 1;
    }
    
    for (int i = m - 2; i >= 0; i--) {
        for (int j = n - 2; j >= 0; j--) {
            int min_health_on_exit = dp[i+1][j] < dp[i][j+1] ? dp[i+1][j] : dp[i][j+1];
            dp[i][j] = min_health_on_exit - dungeon[i][j];
            if (dp[i][j] <= 0) dp[i][j] = 1;
        }
    }
    
    return dp[0][0];
}
相关推荐
三品吉他手会点灯1 小时前
C语言学习笔记 - 50.流程控制4 - 流程控制为什么非常非常重要
c语言·开发语言·笔记·学习
JAVA面经实录9172 小时前
Java 数据结构与算法 (终极完整学习文档)
java·数据结构·算法
开源Z4 小时前
LeetCode 42 · 接雨水:从暴力到双指针的三步优化
算法·leetcode
旖-旎4 小时前
《LeetCode 695 岛屿的最大面积 FloodFill DFS 解法》
c++·算法·力扣·深度优先遍历·floodfill
syagain_zsx5 小时前
STL 之 vector 讲练结合
c++·算法
十月的皮皮5 小时前
C语言学习笔记20260615-有序升序序列合并
c语言·笔记·学习
MartinYeung56 小时前
[论文学习]DP2Unlearning:高效且具保证的大型语言模型遗忘框架(基于差分隐私的 LLM Unlearning 方法)
学习·算法·语言模型
Tian_Hang6 小时前
C++原型模式(Protype)
开发语言·c++·算法
bIo7lyA8v6 小时前
算法复杂度的渐进分析与实际运行时间的差异的技术8
算法