LeetCode刷题笔录dungeon game
来源:互联网 发布:万象数据库修改工具 编辑:程序博客网 时间:2024/06/08 01:09
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。问题就是DP的递归方程怎么写。
dp[i][j]表示进入这个格子后保证knight不会死所需要的最小HP。如果一个格子的值为负,那么进入这个格子之前knight需要有的最小HP是-dungeon[i][j] + 1.如果格子的值非负,那么最小HP需求就是1.
这里可以看出DP的方向是从最右下角开始一直到左上角。首先dp[m-1][n-1] = Math.max(1, -dungeon[m-1][n-1] + 1).
递归方程是dp[i][j] = Math.max(1, Math.min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j]).
public class Solution { public int calculateMinimumHP(int[][] dungeon) { if(dungeon.length == 0) return 0; int m = dungeon.length, n = dungeon[0].length; int[][] dp = new int[m][n]; dp[m - 1][n - 1] = dungeon[m - 1][n - 1] >= 0 ? 1 : -dungeon[m - 1][n - 1] + 1; for(int i = m - 2; i >= 0; i--){ dp[i][n - 1] = Math.max(dp[i + 1][n - 1] - dungeon[i][n - 1], 1); } for(int j = n - 2; j >= 0; j--){ dp[m - 1][j] = Math.max(dp[m - 1][j + 1] - dungeon[m - 1][j], 1); } 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 + 1][j], dp[i][j + 1]) - dungeon[i][j]); } } return dp[0][0]; }}
- LeetCode刷题笔录dungeon game
- LeetCode刷题笔录 Jump Game II
- LeetCode刷题笔录Jump Game
- leetcode -- Dungeon Game -- dp重点,典型题
- [leetcode] Dungeon Game
- leetcode 174: Dungeon Game
- [leetcode 174] Dungeon Game
- [leetcode] Dungeon Game
- Leetcode Dungeon Game
- LeetCode Dungeon Game
- LeetCode: Dungeon Game
- [LeetCode] Dungeon Game
- [leetcode] Dungeon Game
- LeetCode Dungeon Game
- [LeetCode]Dungeon Game
- Leetcode OJ Dungeon Game
- Leetcode: Dungeon Game
- leetcode dungeon game
- 远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
- Ruby Thin服务器——比Rails自带的Webrick快不止一个档次!
- 串口通信资料整理
- Maximum Subarray问题
- leetcode做题总结,动态规划II(Maximum Subarray, Maximum Product Subarray)
- LeetCode刷题笔录dungeon game
- linux ntfs磁盘挂载
- Min Stack
- Leetcode - Reverse Integer
- leetcode-3 closest
- leetcode-4sum
- leetcode-generate parentheses
- 2015-1-7
- 新浪微博数据挖掘食谱之十二: 用户篇 (批量获取用户的粉丝数和朋友数)