Java for LeetCode 174 Dungeon Game
2015-06-06 17:43
567 查看
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.
解题思路:
dp问题,有两种思路:
思路一:dp[i][j]表示从起点到dungeon[i][j]所需的最小血量,但是这种思路递推方程非常不好写
思路二:dp[i][j]表示从dungeon[i][j]到终点所需的最小血量,使用一维数组即可,递推方程
dp[j] = dungeon[i][j] >= Math.min(dp[j + 1], dp[j]) - 1 ? 1: Math.min(dp[j + 1], dp[j]) - dungeon[i][j]
当然,也可以直接用dungeon[i][j]数组替代dp[]数组,JAVA实现如下:
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.
解题思路:
dp问题,有两种思路:
思路一:dp[i][j]表示从起点到dungeon[i][j]所需的最小血量,但是这种思路递推方程非常不好写
思路二:dp[i][j]表示从dungeon[i][j]到终点所需的最小血量,使用一维数组即可,递推方程
dp[j] = dungeon[i][j] >= Math.min(dp[j + 1], dp[j]) - 1 ? 1: Math.min(dp[j + 1], dp[j]) - dungeon[i][j]
当然,也可以直接用dungeon[i][j]数组替代dp[]数组,JAVA实现如下:
public int calculateMinimumHP(int[][] dungeon) { int[] dp = new int[dungeon[0].length]; dp[dungeon[0].length - 1] = dungeon[dungeon.length - 1][dungeon[0].length - 1] >= 0 ? 1 : 1 - dungeon[dungeon.length - 1][dungeon[0].length - 1]; for (int i = dungeon[0].length - 2; i >= 0; i--) dp[i] = dungeon[dungeon.length - 1][i] >= dp[i + 1] - 1 ? 1 : dp[i + 1] - dungeon[dungeon.length - 1][i]; for (int i = dungeon.length - 2; i >= 0; i--) { dp[dungeon[0].length - 1] = dungeon[i][dungeon[0].length - 1] >= dp[dungeon[0].length - 1] - 1 ? 1 : dp[dungeon[0].length - 1] - dungeon[i][dungeon[0].length - 1]; for (int j = dungeon[0].length - 2; j >= 0; j--) dp[j] = dungeon[i][j] >= Math.min(dp[j + 1], dp[j]) - 1 ? 1 : Math.min(dp[j + 1], dp[j]) - dungeon[i][j]; } return dp[0]; }
相关文章推荐
- Leetcode Longest Common Prefix (java)
- 学习Java的5个必经阶段
- (二)spring MVC配置
- 最佳新秀SSH十六Struts2它是如何工作的内部
- Java POI导入Excel文件
- Spring @EnableWebMvc
- springMVC bootstarp 用户权限基础框架+即时通讯
- Java读取属性文件简洁工具类
- Eclipse快捷键
- java 生成单号
- NetBeans 下使用EJB的例子
- JVM、Java编译器和Java解释器
- Eclipse上传项目到git.oschina
- Spring事务配置的五种方式
- 使用JSmooth制造java jar文件可以运行exe文件教程图像
- Java设计模式之单例与双重锁定
- 算法编程(JAVA)--八皇后问题
- java学习【课后笔记】
- 【Thinking in Java笔记】Java环境变量CLASSPATH
- leetcode--Rotate Image