LeetCode Dungeon Game
2015-01-28 16:01
309 查看
Problem:
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
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.
Solution:
采用 Dynamic Programming 可以解决这一问题
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) |
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.
Solution:
采用 Dynamic Programming 可以解决这一问题
class Solution: # @param dungeon, a list of lists of integers # @return a integer def calculateMinimumHP(self, dungeon): mat = dungeon m = len(dungeon) n = len(dungeon[0]) k = max(n, m) for i in range(1,k): if n-1-i >= 0 and m-1-i >= 0: for j in range(i): if mat[m-1-i][n-1-j] <= 0: mat[m-1-i][n-1-j] += min(0,mat[m-i][n-1-j]) if (j == 0) else max(min(0,mat[m-i][n-1-j]), min(0,mat[m-i-1][n-j])) else: mat[m-1-i][n-1-j] += mat[m-i][n-1-j] if (j == 0) else max(mat[m-i][n-1-j], mat[m-i-1][n-j]) if mat[m-1-j][n-1-i] <= 0: mat[m-1-j][n-1-i] += min(0,mat[m-1-j][n-i]) if (j == 0) else max(min(0,mat[m-1-j][n-i]), min(0,mat[m-j][n-1-i])) else: mat[m-1-j][n-1-i] += mat[m-1-j][n-i] if (j == 0) else max(mat[m-1-j][n-i],mat[m-j][n-1-i]) if mat[m-1-i][n-1-i] <= 0: mat[m-1-i][n-1-i] += max(min(0,mat[m-i][n-i-1]), min(0,mat[m-1-i][n-i])) else: mat[m-1-i][n-1-i] += max(mat[m-i][n-i-1], mat[m-1-i][n-i]) elif n-1-i >= 0: for j in range(m): if mat[m-1-j][n-1-i] <= 0: mat[m-1-j][n-1-i] += min(0,mat[m-1-j][n-i]) if (j == 0) else max(min(0,mat[m-1-j][n-i]), min(0,mat[m-j][n-1-i])) else: mat[m-1-j][n-1-i] += mat[m-1-j][n-i] if (j == 0) else max(mat[m-1-j][n-i], mat[m-j][n-1-i]) else: for j in range(n): if mat[m-1-i][n-1-j] <= 0: mat[m-1-i][n-1-j] += min(0,mat[m-i][n-1-j]) if (j == 0) else max(min(0,mat[m-i][n-1-j]), min(0,mat[m-1-i][n-j])) else: mat[m-1-i][n-1-j] += mat[m-i][n-1-j] if (j == 0) else max(mat[m-i][n-1-j], mat[m-1-i][n-j]) return mat[0][0] > 0 and 1 or -mat[0][0] + 1
相关文章推荐
- LeetCode Dungeon Game
- Leetcode Dungeon Game
- LeetCode Dungeon Game
- LeetCode Dungeon Game
- 174. Dungeon Game Leetcode Python
- leetcode之Dungeon Game
- leetcode 174. Dungeon Game 完美世界秋招编程题2
- Leetcode Dungeon Game Java
- LeetCode 174: Dungeon Game 解题与思考
- Dungeon Game LeetCode
- leetcode-dungeon-game
- leetcode dungeon game
- Leetcode OJ Dungeon Game
- Leetcode Dungeon Game(经典动态规划)
- LeetCode Dungeon Game
- Leetcode@jump Game
- leetcode_292_Nim Game
- leetcode之jump game II
- Dungeon Game 解题心得
- Leetcode_jump-game-ii(c++ and python version)