LeetCode 题解(83): 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)-33
-5-101
1030-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.
题解:

用另一个二维数组(一维数组滚动实现)minHP[i][j]记录在进入地牢dungeon[i][j]前保证不死的最低生命值,minHP的最后一个元素值为 max(1 - dungeon[row-1][col-1], 1),即,若dungeon[row-1][col-1] >= 0,最小HP为1即可,若dungeon[row-1][col-1] < 0,则最小HP为 1 - dungeon[row-1][col-1]。

逆向求minHP的其它元素:minHP[i][j] = max(min(minHP[i+1][j], minHP[i][j+1]) - dungeon[i][j], 1)。最后结果为minHP[0][0],即保证最后不死的进入地牢的最小生命值。

c++版:

class Solution {
public:
    int calculateMinimumHP(vector<vector<int> > &dungeon) {
        if(dungeon.size() == 0)
            return 1;
        
        int** minHP = new int*[dungeon.size()];
        
        for(int i = 0; i < dungeon.size(); i++) {
            minHP[i] = new int[dungeon[0].size()];
        }
        
        minHP[dungeon.size()-1][dungeon[0].size()-1] = max(1-dungeon[dungeon.size()-1][dungeon[0].size()-1], 1);
        
        for(int col = dungeon[0].size() - 2; col >= 0; col--) {
            minHP[dungeon.size()-1][col] = max(minHP[dungeon.size()-1][col+1] - dungeon[dungeon.size()-1][col], 1);
        }
        
        for(int row = dungeon.size() - 2; row >= 0; row--) {
            for(int col = dungeon[0].size() - 1; col >= 0; col--) {
                if(col == dungeon[0].size() - 1) {
                    minHP[row][col] = max(minHP[row+1][col] - dungeon[row][col], 1);
                    continue;
                }
                minHP[row][col] = max(min(minHP[row+1][col], minHP[row][col+1]) - dungeon[row][col], 1);
            }
        }
        return minHP[0][0];
    }
};

Java版:

public class Solution {
    public int calculateMinimumHP(int[][] dungeon) {
        if(dungeon.length == 0)
            return 1;
        int[][] minHP = new int[dungeon.length][dungeon[0].length];
        
        minHP[dungeon.length-1][dungeon[0].length-1] = Math.max(1 - dungeon[dungeon.length-1][dungeon[0].length-1], 1);
        
        for(int row = dungeon.length - 1; row >= 0; row--) {
            for(int col = dungeon[0].length - 1; col >= 0; col--) {
                if(row == dungeon.length - 1 && col == dungeon[0].length - 1)
                    continue;
                else if(row == dungeon.length - 1) {
                    minHP[row][col] = Math.max(minHP[row][col+1] - dungeon[row][col], 1);
                }
                else if(col == dungeon[0].length - 1) {
                    minHP[row][col] = Math.max(minHP[row+1][col] - dungeon[row][col], 1);
                } else {
                    minHP[row][col] = Math.max(Math.min(minHP[row][col+1], minHP[row+1][col]) - dungeon[row][col], 1);
                }
            }
        }
        return minHP[0][0];
    }
}

Python版:

class Solution:
    # @param dungeon, a list of lists of integers
    # @return a integer
    def calculateMinimumHP(self, dungeon):
        if len(dungeon) == 0:
            return 1
        l = []
        for i in range(0, len(dungeon)):
            l.append([0] * len(dungeon[0]))
        
        l[len(dungeon)-1][len(dungeon[0])-1] = max(1 - dungeon[len(dungeon)-1][len(dungeon[0])-1], 1)
        for row in range(len(dungeon) - 1, -1, -1):
            for col in range(len(dungeon[0]) - 1, -1, -1):
                if row == len(dungeon) - 1 and col == len(dungeon[0]) - 1:
                    continue
                elif row == len(dungeon) - 1:
                    l[row][col] = max(l[row][col+1] - dungeon[row][col], 1)
                elif col == len(dungeon[0]) - 1:
                    l[row][col] = max(l[row+1][col] - dungeon[row][col], 1)
                else:
                    l[row][col] = max(min(l[row+1][col], l[row][col+1]) - dungeon[row][col], 1)
        return l[0][0]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值