LeetCode 174. Dungeon Game(游戏)
2016-05-25 01:35
337 查看
原题网址: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
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.
方法:动态规划,倒推。
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.
方法:动态规划,倒推。
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] = 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 187. Repeated DNA Sequences(重复DNA序列)
- LeetCode 351. Android Unlock Patterns(安卓解锁)
- C#匹配中文字符串的4种正则表达式分享
- Redis中基本数据类型的操作
- Android 简单的条形颜色选择器
- 独立看门狗
- 匹配中文字符的正则表达式: [/u4e00-/u9fa5]
- xml解析之SAX
- Restful接口设计
- 思
- JavaWeb会话管理:(2)Session
- This Handler class should be static or leaks may occur (anonymous android.os.Handler)
- ibdata文件增大的原因
- Linux FTP的安装与配置
- linux 安装ffmpeg记录
- HDU-1874-畅通工程续(随便你什么算法,反正我是睡不着)
- 组合简便算法 (long long 与__int64)
- 64_鸡兔同笼
- sublime text 3 3114 注册码
- ubuntu15.10+fastDFS_v5.08配置