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 ...
随机推荐
- Linux 虚拟机和物理机配互信出现无法连接
配置文件位置:[root@hank-yoon data]# vi /etc/ssh/sshd_configPermitRootLogin yes 在物理机中,装完系统,默认情况下PermitRootL ...
- 透过数据看现实,漫谈实况FIFA的这些年
虽然,只是个普通玩家,虽然带了一点青春,一点爱.虽然,有那么些怀念 ~ 好吧,不浪费篇幅伪伪的煽情,直插主题.(很长且多图,更多讲述的是实况FIFA间的你来我往,互相赶超的故事.本想全面展开描述细节, ...
- Android--WebView显示Html,让其中的图片适应屏幕宽度
//设置 防止图片太大超出屏幕 tv_web_danGe.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COL ...
- [译]rabbitmq 2.4 Multiple tenants: virtual hosts and separation
我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. With exchanges, bindings, and queues under your belt, you mig ...
- [iOS]深入浅出 iOS 之多线程 NSThread
OS 支持多个层次的多线程 编程,层次越高的抽象程度越高,使用起来也越方便,也是苹果最推荐使用的方法. 下面简要说明这三种不同范式: Thread 是这三种范式里面相对轻量级的,但也是使用起 ...
- Memcached 在windows环境下安装
1.memcached简介 memcached是一个高性能的分布式内存对象缓存系统,它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动应用的访问性 能.memcached基于 ...
- SQL Server数据库学习笔记-概念数据模型
概念数据模型(Conceptual Data Model)也称为信息模型.它是对客观事物及其联系的抽象,用于信息世界的建模,是现实世界到信息世界的第一层抽象,是数据库设计人员进行数据库设计的有力工具. ...
- C Primer Plus学习笔记(二)
1. C的左值用是指用于标志一个特定的数据对象的名字或表达式.“数据对象”是泛指数据存储的术语. 赋值运算符的左边应该是以个可以修改的左值. 右值是指可赋给可修gia的左值的量.右值可以是常量.变量或 ...
- 一:java概述:
1991 年Sun公司的James Gosling等人开始开发名称为 Oak 的语言,希望用于控制嵌入在有线电视交换盒.PDA等的微处理器: 1994年将Oak语言更名为Java: Java的三种技术 ...
- cocos中常用到的单例模式
单例:即只有一个类对象,且提供全局的访问权限 特点: 1.构造函数私有 2.私有的静态成员指针,标识是否已产生了单例实例 3.提供一个getInstance()方法来获取单例对象 下面已打飞机中的子弹 ...