【LeetCode】Dungeon Game 解题报告【Solution】
来源:互联网 发布:淘宝网首页电脑版登录 编辑:程序博客网 时间:2024/04/29 13:26
【题目】
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.
有mxn个格子,皇后在右下角,勇士在左上角,现在勇士要去救皇后,每步可以往下或往右走,每个格子里的数字表示勇士到该处时要消耗或增加的魔力,(负数表示消耗,正数表示增加),不论何时只要勇士的魔力小于等于0,勇士立即死掉。问勇士初始至少要有多少魔力?
【解析】
思路:动态规划
用一个二维数组ans[][]表示到每个格子时,勇士到每一步时至少需要的魔力,如ans[i][j]表示勇士在[i, j]处至少需要ans[i][j]魔力才能到达[m, n]救出皇后。
技巧:从[m, n]往回遍历到[1, 1]
动规方程:如果当前位置魔力正且大于等于右边/下边需要的魔力,则该处不需要额外的魔力,否则,勇士到达该处时需有一定的魔力来满足该处和右边/下边需要的魔力。
【Java代码】
public class Solution { public int calculateMinimumHP(int[][] dungeon) { int m = dungeon.length; int n = dungeon[0].length; int[][] ans = new int[m][n]; //初始化最后一行和最后一列 ans[m - 1][n - 1] = dungeon[m - 1][n - 1] > 0 ? 0 : -dungeon[m - 1][n - 1]; for (int i = m - 2; i >= 0; i--) { ans[i][n - 1] = dungeon[i][n - 1] >= ans[i + 1][n - 1] ? 0 : ans[i + 1][n - 1] - dungeon[i][n - 1]; } for (int j = n - 2; j >= 0; j--) { ans[m - 1][j] = dungeon[m - 1][j] >= ans[m - 1][j + 1] ? 0 : ans[m - 1][j + 1] - dungeon[m - 1][j]; } //从右下角往左上角遍历 for (int i = m - 2; i >= 0; i--) { for (int j = n - 2; j >= 0; j--) { int down = dungeon[i][j] >= ans[i + 1][j] ? 0 : ans[i + 1][j] - dungeon[i][j]; int right = dungeon[i][j] >= ans[i][j + 1] ? 0 : ans[i][j + 1] - dungeon[i][j]; ans[i][j] = Math.min(down, right); } } //要保证勇士活着,至少需要1魔力 return ans[0][0] + 1; }}
- 【LeetCode】Dungeon Game 解题报告【Solution】
- LeetCode 解题报告 Dungeon Game
- [leetcode] 174. Dungeon Game 解题报告
- [Leetcode] 174. Dungeon Game 解题报告
- Dungeon Game解题报告
- Dungeon Game | leetcode 解题思路
- Leetcode Jump Game 解题报告
- 【LeetCode】Jump Game 解题报告
- leetcode [Nim game解题报告]
- [LeetCode] Jump Game 解题报告
- 【LeetCode】Nim Game 解题报告
- Dungeon Game 解题心得
- Leetcode Jump Game II 解题报告
- 【LeetCode】Jump Game II 解题报告
- [leetcode] 55. Jump Game 解题报告
- [leetcode] 45. Jump Game II 解题报告
- LeetCode(292) Nim Game解题报告
- [leetcode] 292. Nim Game 解题报告
- Android 获取外置USB的路径
- Search Insert Position -- leetcode
- zoj 3845 Fixed Point(数位dp)
- 使用QT在S3C6410上点亮一个LED灯
- 异常、错误记录
- 【LeetCode】Dungeon Game 解题报告【Solution】
- TCP服务端和客户端(3)
- 【java基础】第九天总结-->重要
- 8属性表对话框
- c#之语言基础
- 20150111Review
- kali linux渗透测试(一) --- set社工攻击
- ubuntu 下SDL的安装
- 帮同学写的ATM机模拟(简单链表操作)