174. Dungeon Game
来源:互联网 发布:手机练钢琴软件 编辑:程序博客网 时间:2024/05/22 06:45
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[0].length==0) return 0; int M=dungeon[0].length; int N=dungeon.length; int[] HP = new int[M]; // int health=1; HP[M-1]=Math.max(1,1-dungeon[N-1][M-1]); for(int j=N-1; j>=0; j--){ for(int i=M-1; i>=0; i--){ if(i==M-1){ if(j!=N-1) HP[i]=Math.max(1,HP[i]-dungeon[j][i]); } else { if(j==N-1) HP[i]=Math.max(1,HP[i+1]-dungeon[j][i]); else HP[i]=Math.max(1, Math.min(HP[i+1],HP[i])-dungeon[j][i]); } } } return HP[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
- Java中Atomic包的原理和分析
- HDU ACM 11 2017 字符串统计
- UVa227
- 使用ExpandableTextView遇到的坑
- Unity ugui拼界面规则
- 174. Dungeon Game
- 使用MVC模型的用户登录及注册(登录部分)
- Jquery中的on()使用理解,无法使用on方法
- 第4课 Android开发工具——Gradle知识汇总
- codeforces 369C
- RecyclerView初步试用(一)
- C#读写文件总结
- Android中的图片缓存
- 106.Convert Sorted List to Balanced BST-排序列表转换为二分查找树(中等题)