【leetcode】174. 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)

 

Note:

  • 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.

解题思路:加了一点点难道的动态规划。对于dungeon[i][j]来说,如果其值大于等于0表示骑士到达这个位置最低的health是1,如果小于0则最低health是1-dungeon[i][j]。而dungeon[i][j]只能通过dungeon[i-1][j]或者dungeon[i][j-1]到达,所以dungeon[i-1][j]/dungeon[i][j-1]既要满足到达自身时healty要满足大于等于1,同时也要满足有足够的health到达dungeon[i][j]。这里很容易就能得到递推表达式:

·dungeon[i-1][j] < 0: 因为dungeon[i-1][j] 小于0,骑士在这里是损耗health的,而且又要保证有足够的health到下一个位置,得出表达式dp[i-1][j] = min(dp[i-1][j],dp[i][j] - dungeon[i-1][j] )

·1 + dungeon[i - 1][j] > dp[i][j]:dungeon[i - 1][j] 是正数,而且足够补充health到下一个位置,因为骑士到这里的health只有满足最小值1即可,得出表达式dp[i - 1][j] = min(dp[i - 1][j], 1)

·其他: 这种情况是dp[i - 1][j]是正数,但是自身不足以补充足够的health到下一个位置,因此需要骑士到达自身的时候自己带一部分health,得出表达式dp[i - 1][j] = min(dp[i - 1][j], dp[i][j] - dungeon[i - 1][j] )

代码如下:

class Solution(object):
    def calculateMinimumHP(self, dungeon):
        """
        :type dungeon: List[List[int]]
        :rtype: int
        """
        dp = []
        for i in dungeon:
            dp.append([float('inf')] * len(i))
        dp[-1][-1] = 1 if dungeon[-1][-1] >= 0 else 1 - dungeon[-1][-1]

        for i in range(len(dungeon) - 1,-1,-1):
            for j in range(len(dungeon[i])-1,-1,-1):
                if i - 1 >= 0 :
                    if dungeon[i-1][j] < 0:
                        dp[i-1][j] = min(dp[i-1][j],dp[i][j] - dungeon[i-1][j] )
                    elif 1 + dungeon[i - 1][j] > dp[i][j]:
                        dp[i - 1][j] = min(dp[i - 1][j], 1)
                    else:
                        dp[i - 1][j] = min(dp[i - 1][j], dp[i][j] - dungeon[i - 1][j] )
                if j - 1 >= 0 :
                    if dungeon[i][j-1] < 0:
                        dp[i][j-1] = min(dp[i][j-1],dp[i][j] - dungeon[i][j-1])
                    elif 1 + dungeon[i][j-1] > dp[i][j]:
                        dp[i][j-1] = min(dp[i][j-1], 1)
                    else:
                        dp[i][j-1] = min(dp[i][j-1], dp[i][j] - dungeon[i][j-1])
        #print dp
        return dp[0][0]

 

posted @ 2019-03-11 10:20  seyjs  阅读(200)  评论(0编辑  收藏  举报