174. Dungeon Game(动态规划)
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 (positiveintegers).
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) |
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.
class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int n = dungeon.length;
int m = dungeon[0].length;
int[][] dp = new int[n+1][m+1];
for(int i=0;i<=n;i++)
for(int j = 0;j<=m;j++)
dp[i][j] = Integer.MAX_VALUE;
dp[n][m-1] = 1;
dp[n-1][m] = 1;
for(int i=n-1;i>=0;i--)
for(int j = m -1;j>=0;j--){
int need = Math.min(dp[i][j+1],dp[i+1][j]) - dungeon[i][j];
dp[i][j] = need<=0?1:need;
}
return dp[0][0];
}
}
174. Dungeon Game(动态规划)的更多相关文章
- [LeetCode] 174. Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Dungeon Game ——动态规划
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 【LeetCode】174. Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- 【LeetCode】174. Dungeon Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Java for LeetCode 174 Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Leetcode#174 Dungeon Game
原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...
- 174. Dungeon Game
题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
随机推荐
- jpa中时间戳格式应该用哪种类型
遇到个bug,数据库时间存储用了datetime,但是下面的java jpa代码,查询回来,却只有日期. String innerSql = getInnerQuery(departmentId, k ...
- mac下用户用户组命令行操作
使用mac的时候需要像linux一样对用户和群组进行操作,但是linux使用的gpasswd和usermod在mac上都不可以使用,mac使用dscl来对group和user操作. 查看用户组: ds ...
- vscode的vetur插件提示 [vue-language-server] Elements in iteration expect to have 'v-bind:key' directives错误的解决办法
1.使用VS Code 出现如下问题,如图 Vue 2.2.0+的版本里,当在组件中使用v-for时,key是必须的. 2.更改vetur配置 vscode->文件->首选项->用户 ...
- C语言如何产生随机数
1.基本函数 在C语言中取随机数所需要的函数是: int rand(void); void srand(unsigned int n); rand()函数和srand()函数被声明在头文件stdlib ...
- 通过JS模拟select表单,达到美化效果[demo][转]
转自: http://www.cnblogs.com/dreamback/p/SelectorJS.html 通过JS模拟select表单,达到美化效果 Demo ------------------ ...
- ajax跨域获取返回值
js代码 $.ajax({ async:false, url: 'https://***/api/prepareApi.getDanMu?sqlMapId=findBarrage', // 跨域URL ...
- HDU-1394 Minimum Inversion Number(线段树求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- Pymongo NotMasterError while fetching count of the collection as per query from MongoDB in DRF
django rest framework - Pymongo NotMasterError while fetching count of the collection as per query f ...
- textField placeholder颜色,位置设置
自定义textField继承自UITextField 重写 - (CGRect)placeholderRectForBounds:(CGRect)bounds _phoneTF.font = HPFo ...
- Jena 操作 RDF 文件
1. RDF 入门 RDF(Resource Description Framework)是由W3C规定的,描述资源(resource)的数据模型(data model),: RDF 使用Web标识符 ...