leetcode题解-174. Dungeon Game
来源:互联网 发布:淘宝通知买家虚假交易 编辑:程序博客网 时间:2024/06/03 15:50
题目:
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 110 30 -5 (P)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.
本题是寻找经过迷宫所需要的最少血量。每走一步都会减血或者加血,但是血量一旦少于或者等于0,就会死亡。由于只能向右或者向下走,所以第一行和第一列都只有一种走法。我们可以从右下角反向遍历,并使用一个相同大小的矩阵来记录当前所需要的血量。代码如下所示:
public int calculateMinimumHP2(int[][] dungeon) { if (dungeon == null || dungeon.length == 0 || dungeon[0].length == 0) return 0; int m = dungeon.length; int n = dungeon[0].length; //额外一个矩阵记录当前所需最低血量 int[][] health = new int[m][n]; //如果dungeon中该元素为负,则取绝对值加一,否则取一即可。 health[m - 1][n - 1] = Math.max(1 - dungeon[m - 1][n - 1], 1); for (int i = m - 2; i >= 0; i--) { health[i][n - 1] = Math.max(health[i + 1][n - 1] - dungeon[i][n - 1], 1); } for (int j = n - 2; j >= 0; j--) { health[m - 1][j] = Math.max(health[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--) { int down = Math.max(health[i + 1][j] - dungeon[i][j], 1); int right = Math.max(health[i][j + 1] - dungeon[i][j], 1); health[i][j] = Math.min(right, down); } } return health[0][0]; }
除此之外,我们可以遍历所有遍历的路径然后计算该条路径所需最少血量,遍历完所有路径之后即可得到答案,这种方法可以击败99%的用户,其实很多这种走迷宫之类的问题都可以使用这种DP的思路来求解,每个函数内部分别调用向下和向右两次函数即可。代码如下:
public int calculateMinimumHP1(int[][] dungeon) { if(dungeon == null || dungeon.length == 0 || dungeon[0].length == 0){//[],[[]] return 0; } int[][] flag = new int[dungeon.length][dungeon[0].length]; return dfs(dungeon, flag, 0, 0); } private int dfs(int[][] dungeon, int[][] flag, int x, int y){ if(flag[x][y] != 0){ return flag[x][y]; } if(x == dungeon.length - 1 && y == dungeon[0].length - 1){//The down-right corner flag[x][y] = dungeon[x][y] < 0 ? -dungeon[x][y] + 1 : 1; //The minimum is 1 return flag[x][y]; } int min = Integer.MAX_VALUE; //go down if(x < dungeon.length - 1){ int down = dfs(dungeon, flag, x + 1, y); min = min < down ? min : down; } //go right if(y < dungeon[0].length - 1){ int right = dfs(dungeon, flag, x, y + 1); min = min < right ? min : right; } if(dungeon[x][y] >= min){//If min is 6, dungeon[x][y] if 10, then min should be updated to 1 min = 1; }else{//If min is 6, dungeon[x][y] is 3 or -3, then min should be updated to 3 or 9 min = min - dungeon[x][y]; } flag[x][y] = min; return min; }
阅读全文
0 0
- leetcode题解-174. Dungeon Game
- LeetCode 174 Dungeon Game 题解
- [leetcode] 174.Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- LeetCode-174.Dungeon Game
- [LeetCode] 174. Dungeon Game
- LeetCode 174. Dungeon Game
- LeetCode 174. Dungeon Game
- [LeetCode]174. Dungeon Game
- leetCode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- Leetcode 174. Dungeon Game
- [leetcode] 174. Dungeon Game
- leetcode 174. Dungeon Game
- LeetCode 题解(83): Dungeon Game
- 174. Dungeon Game Leetcode Python
- 检查iOS项目中是否使用了IDFA
- spring框架
- android 反射调用实现mount/unmout 自动弹出、装载功能
- 阿里云ubuntu16.04 配置jdk1.7 开发环境
- Mybatis映射文件sql语句注意事项(个人笔记)
- leetcode题解-174. Dungeon Game
- mybatis(5)---如何解决实体类中字段和数据表字段不对应引发的问题
- 移动端混合开发(1):和H5的javascript交互
- @property--属性函数
- PHP和golang实现设计模式-工厂方法模式
- 欢迎使用CSDN-markdown编辑器
- 国外的 iOS 开发者是怎么看中国的?(上)
- 文本深度表示模型—word2vec&doc2vec词向量模型
- 谈谈对Spring IOC的理解