174. Dungeon Game
来源:互联网 发布:中央电视台网络客户端 编辑:程序博客网 时间:2024/06/10 01:49
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.
这个题用DP是obvious,但是要从右下角往左上角DP,刚开始我是这样写的
/* * DP:一个位置只保存一个貌似是不行的 * dp表示到达该点时最少需要的血点数,dp1表示到达该点后从右走,dp2从下走 * DFS */public class CopyOfSolution { public int calculateMinimumHP(int[][] dungeon) { int m = dungeon.length, n = dungeon[0].length; int[][] dp1 = new int[m][n], dp2 = new int[m][n]; dp1[m-1][n-1] = Math.max(1, 1 - dungeon[m-1][n-1]); dp2[m-1][n-1] = Math.max(1, 1 - dungeon[m-1][n-1]); for(int i=m-2; i>=0; i--) { dp1[i][n-1] = Integer.MAX_VALUE; dp2[i][n-1] = Math.max(1, dp2[i+1][n-1] - dungeon[i][n-1]); } for(int i=n-2; i>=0; i--) { dp2[m-1][i] = Integer.MAX_VALUE; dp1[m-1][i] = Math.max(1, dp1[m-1][i+1] - dungeon[m-1][i]); } for(int i=m-2; i>=0; i--) { for(int j=n-2; j>=0; j--) { dp1[i][j] = Math.max(1, Math.min(dp1[i][j+1], dp2[i][j+1]) - dungeon[i][j]); dp2[i][j] = Math.max(1, Math.min(dp1[i+1][j], dp2[i+1][j]) - dungeon[i][j]); } } return Math.min(dp1[0][0], dp2[0][0]); }}
其实建两个数组完全没有必要,一个就够了,到i,j为止的最优,然后继续往后根据之前的最优寻找最优,还是DP算法不熟
public class Solution { public int calculateMinimumHP(int[][] dungeon) { int m = dungeon.length, n = dungeon[0].length; int[][] dp = new int[m][n]; dp[m-1][n-1] = Math.max(1, 1 - dungeon[m-1][n-1]); for(int i=m-2; i>=0; i--) { dp[i][n-1] = Math.max(1, dp[i+1][n-1] - dungeon[i][n-1]); } for(int i=n-2; i>=0; i--) { dp[m-1][i] = Math.max(1, dp[m-1][i+1] - dungeon[m-1][i]); } for(int i=m-2; i>=0; i--) { for(int j=n-2; j>=0; j--) { dp[i][j] = Math.max(1, Math.min(dp[i][j+1], dp[i+1][j]) - dungeon[i][j]); } } return dp[0][0]; }}
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- 174. Dungeon Game
- 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- 174. Dungeon Game
- leetCode 174. Dungeon Game
- 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- 20161209计算机科学导论03_内存
- js之间是如何调用的?比如自己写的js,怎么调用jQuery框架中的js
- 如何通过包名来获取下面所有的class文件
- android apk的反编译的相关操作
- loadRunner:microsoft visual c++ 2005 sp1运行时组件安装不成功,这是什么情况
- 174. Dungeon Game
- 通过Spring获取properties文件属性值
- vs2015安装记录2016年8月3日
- win7部署kafka_2.11
- 并查集(合作网络,LA 3027)
- 【Java面试题-001】什么是JVM?为什么称Java为跨平台的编程语言?
- 20161209 spring 跟马士兵学习记录
- 资源Resources
- Linux进程之初步了解