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 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.
二维DP,从底向上扫。设D[i,j]为走进房间[i,j]之前拥有,并且能够成功到达右下角所需最小HP。由于走进房间[i,j]的下一步要么走进[i,j+1],要么走进[i+1,j],所以选择D[i,j+1],D[i+1,j]中间比较小的那个作为出房间[i,j]的最小HP。所以加上房间本身的补给或是伤害之后:D[i,j] =max(1, min(D[i,j+1],D[i+1,j])-dungeon[i,j])
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int m = dungeon.length, n = dungeon[0].length;
int[][] arr = new int[m][n];
arr[m - 1][n - 1] = dungeon[m - 1][n - 1] >= 0 ? 1 : 1 - dungeon[m - 1][n - 1];
for(int i = m - 2; i > -1; i --){
arr[i][n - 1] = Math.max(arr[i + 1][n - 1] - dungeon[i][n - 1], 1);
}
for(int j = n - 2; j > -1; j --){
arr[m - 1][j] = Math.max(arr[m - 1][j + 1] - dungeon[m - 1][j] , 1);
}
for(int i = m - 2; i > -1; i --){
for(int j = n - 2; j > -1; j --){
int before = Math.min(arr[i + 1][j], arr[i][j + 1]);
arr[i][j] = Math.max(before - dungeon[i][j], 1);
}
}
return arr[0][0];
}
}
Dungeon Game的更多相关文章
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- leetcode174. Dungeon Game
// learn from https://discuss.leetcode.com/topic/6912/c-dp-solution ''' class Solution { public: int ...
- 【leetcode】Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- Dungeon Game ——动态规划
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 Dungeon Game
原题链接在这里:https://leetcode.com/problems/dungeon-game/ 这是一道DP题,保存当前格到右下格所需要的最小体力,m*n的dp数组保存. 更新是Math.mi ...
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20995 Accepted: 8150 Descr ...
随机推荐
- 【Django】Apache上运行多个Django项目
运行单个项目的步骤参考:这里 1 安装环境 操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中) python 版本: Python 2.7.3 Django版本 > ...
- Activity(一)
一个应用程序中至少包含一个Activity,Activity启动流程:当启动一个应用程序时,android操作系统会访问该应用程序的AndroidManifest.xml文件(该文件中说明了应用程序使 ...
- SQL基础篇----select语句与排序问题
一.检索--输出所有的列 SELECT * FROM my_friends WHEREfirst_name = 'cake'; 知识点1 * 代表选择出所有的行-----(什么行呢?)就是first_ ...
- 苏泊尔借助微软CRM提升客户满意度
企业背景 作为中国最大.全球第二的炊具研发制造商和中国小家电领先品牌,品质和创新一是苏泊尔矢志追求的企业理念,从火红点无油烟锅的发明到能做柴火饭的球釜IH饭煲的面世,苏泊尔用产品的创新和品质的承诺,不 ...
- Python数学运算
python中的加减乘除比其他的语言简单,不需要对其赋值变量 (1)加减乘除 ) #加法 ) #减法 ) #乘法 ) #除法 5.0 ) #乘方 (2)判断 判断返回的是True或者False ) # ...
- RMAN - 备份异机恢复
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...
- Javascript中“==”与“===”的区别
在Javascript中有"=="和"==="两种比较运行符,那么他们有什么区别呢? 一.对于string,number等基础类型,==和===是有区别的 1) ...
- DWR在Spring中应用
这里以传递一个对象为例,来说明dwr在Spring中是怎么配置的. JSP页面: <script src='dwr/interface/instructionOuterService.js'&g ...
- angularjs+nodejs+mongodb三件套
说实话,自己对于web前段的认识并不是太深入,但是因为项目的需要,所以有的时候肯定会需要接触到web前段的知识点.说到web前端想必大家肯定会想到css+js+html,的确web前端的工作,从某总角 ...
- vnextcn
Flag 标题 通过 提交 AC% 难度 E1 编写Hello World网站 9 17 52% 1 E2 编写Hello World控制台程序 11 13 84% 1 E3 控制器基础练 ...