[LeetCode] 174. Dungeon game

Topic links: https://leetcode-cn.com/problems/dungeon-game/

Subject description:

Some devil seize the princess (P) and locked her in the lower right corner of the dungeon. Dungeons is a two-dimensional grid of M x N consisting of rooms. Our brave knight (K) was originally placed in the upper left corner of the room, he must pass through the dungeons and fight the demons to rescue the princess.

Knight's initial health points is a positive integer. If his health points at a time to 0 or less, he would die immediately.

Some rooms are guarded by the devil, so the Cavaliers when entering these rooms will lose health points (if negative integer value of the room, then the knight will lose health points); other rooms are either empty (the room is 0) or contains a healthy increase in the number of points knight magic ball (if the room is a positive integer, then the knight will increase health points).

In order to reach the princess as soon as possible, a time Knight decided to move one step to the right or down.

Write a function to compute ensure knight could save the princess required minimum initial health points.

Example:

For example, consider the following layout of the underground city, the best path to follow if Knight Right -> Right -> next -> next, the Knights of the initial health of at least 7 points.

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

Description:

  • Knight's health points is no upper limit.
  • Any room could pose a threat to the health of the Knights points, may also increase the Knights of health points, including the Knights into the upper left corner of the room and the lower right corner of the room princess imprisoned.

Ideas:

This question of how meaning is from top left to bottom right, only the right, down and in between there is blood, there is buckle blood, blood makes just how initial arrival.

In fact, we may want to reverse. How much blood from the lower right to maintain to reach the upper left corner. How Lane?

We dp[i][j]said in i,jthe blood position requires a minimum of

Dynamic equation:dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j])

To consider the border because the border is only one choice (either downwards, or to the right)

code show as below:

class Solution:
    def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
        row = len(dungeon)
        col = len(dungeon[0])
        dp = [[0] * col for _ in range(row)]
        # 初始化 右下角最少血量
        dp[-1][-1] = max(1 - dungeon[-1][-1], 1)
        # 行
        for i in range(col - 2, -1, -1):
            dp[-1][i] = max(1, dp[-1][i + 1] - dungeon[-1][i])
        # 列
        for i in range(row - 2, -1, -1):
            dp[i][-1] = max(1, dp[i + 1][-1] - dungeon[i][-1])
        #print(dp)
        for i in range(row - 2, -1, -1):
            for j in range(col - 2, -1, -1):
                dp[i][j] = max(min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j], 1)
        return dp[0][0]

Guess you like

Origin www.cnblogs.com/powercai/p/11328813.html