LeetCode 174. Dungeon Game(游戏)
来源:互联网 发布:node导出excel 编辑:程序博客网 时间:2024/06/06 03:03
原题网址:https://leetcode.com/problems/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
.
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.
方法:动态规划,倒推。
public class Solution { public int calculateMinimumHP(int[][] dungeon) { if (dungeon == null || dungeon.length == 0 || dungeon[0].length == 0) return 1; int m = dungeon.length; int n = dungeon[0].length; int[][] hp = new int[m+1][n+1]; Arrays.fill(hp[m], Integer.MAX_VALUE); hp[m][n-1] = 1; for(int i=m-1; i>=0; i--) { hp[i][n] = Integer.MAX_VALUE; for(int j=n-1; j>=0; j--) { int need = Math.min(hp[i][j+1], hp[i+1][j]); hp[i][j] = Math.max(1, need-dungeon[i][j]); } } return hp[0][0]; }}
- LeetCode 174. Dungeon Game(游戏)
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- leetCode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- 174. Dungeon Game Leetcode Python
- [Leetcode]174. Dungeon Game @python
- [LeetCode]problem 174. Dungeon Game
- LeetCode 170. Two Sum III - Data structure design(两数和)
- LeetCode 171. Excel Sheet Column Number(Excel列号)
- LeetCode 172. Factorial Trailing Zeroes(0结尾)
- LeetCode 173. Binary Search Tree Iterator(二叉搜索树迭代器)
- LeetCode 254. Factor Combinations
- LeetCode 174. Dungeon Game(游戏)
- LeetCode 179. Largest Number(最大数)
- A slave with the same server_uuid as this slave has connected to the master
- Anaconda+Theano+Keras安装
- 前端实践--JavaScript--动画(三)
- Android Fragment 真正的完全解析(上)
- Android Fragment 真正的完全解析(下)
- Http协议原理解析第一篇
- servlet监听器