Dungeon Game 解答
Question
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.
Solution
这道题一看就知道用DP做。但是如果是从(0,0)开始推到(m - 1, n - 1),对于每个点,我们需要纪录两个值: 1. 走到该点需要的最少血量 2. 该点富余的血量。这样,我们无法得到一个判断标准。
所以,我们从反方向出发。从(m - 1, n - 1)到(0,0),hp[i][j] 代表从(i,j)走到终点需要的最少血量。如果dungeon[i][j]的值为负,那么进入这个格子之前knight需要有的最小HP是-dungeon[i][j] + min(left, right).如果格子的值非负,那么最小HP需求就是1。
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int m = dungeon.length, n = dungeon[0].length;
int[][] hp = new int[m][n];
hp[m - 1][n - 1] = Math.max(-dungeon[m - 1][n - 1] + 1, 1);
for (int i = m - 2; i >= 0; i--) {
hp[i][n - 1] = Math.max(-dungeon[i][n - 1] + hp[i + 1][n - 1], 1);
}
for (int i = n - 2; i >= 0; i--) {
hp[m - 1][i] = Math.max(-dungeon[m - 1][i] + hp[m - 1][i + 1], 1);
}
for (int i = m - 2; i >= 0; i--) {
for (int j = n - 2; j >= 0; j--) {
hp[i][j] = Math.max(1, -dungeon[i][j] + Math.min(hp[i + 1][j], hp[i][j + 1]));
}
}
return hp[0][0];
}
}
Dungeon Game 解答的更多相关文章
- 动态规划——Dungeon Game
这又是个题干很搞笑的题目:恶魔把公主囚禁在魔宫的右下角,骑士从魔宫的左上角开始穿越整个魔宫到右下角拯救公主,为了以最快速度拯救公主,骑士每次只能向下或者向右移动一个房间, 每个房间内都有一个整数值,负 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- [LeetCode] Dungeon Game 地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
- 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- 【字符编码】Java字符编码详细解答及问题探讨
一.前言 继上一篇写完字节编码内容后,现在分析在Java中各字符编码的问题,并且由这个问题,也引出了一个更有意思的问题,笔者也还没有找到这个问题的答案.也希望各位园友指点指点. 二.Java字符编码 ...
- spring-stutrs求解答
这里贴上applicationContext里的代码: <?xml version="1.0" encoding="UTF-8"?> <bea ...
- JavaScript Bind()趣味解答 包懂~~
首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...
随机推荐
- Android 之窗口小部件详解(三) 部分转载
原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...
- poj 2046 Gap(bfs+hash)
Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...
- Web Service工作原理
Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...
- iphone开发之适配iphone5
iphone5出来了,从不用适配的我们也要像android一样适配不同分辨率的屏幕了. 公司产品新版本需要适配iphone5,经过一番折腾算是搞定了.下面分享给大家: iphone5的屏幕分辨 ...
- PHP 生成.csv 文件并下载到浏览器
近期做了一个项目须要把订单的信息显示出来.而且可以把相关信息放到一个.csv 文件里,下载到浏览器.首先我要说明的是.csv 文件,PHP 有专门的函数去解析该类型的文件,相关函数大家可以去官网查看. ...
- Linux查看系统状态及备份
1. 如何看当前Linux系统有几颗物理CPU和每颗CPU的核数?cat /proc/cpuinfo将CPU的总核数除以物理CPU的个数,得到每颗CPU的核数.2. 查看系统负载有两个常用的命令,是哪 ...
- C#读取XML配置文件
DataSource.xml文件,要放在bin/debug/目录下: <?xml version="1.0" encoding="utf-8" ?> ...
- ios9API基础知识总结(二)
UIAlertView(警告框) UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"标题" message:@&qu ...
- ADO.NET DataReader和DataAdapter的区别
SqlDataReader是一个向前的指针,本身并不包含数据,调用一次 Read() 方法它就向前到下一条记录,一个SqlDataReader必须单独占用一个打开的数据库连接. 在使用 SqlData ...
- C#基本语句与C++区别
条件语句必须为bool表达式 int a = 1; if(a) { ... } 在c++中可以,但c#报错 但 bool b = true;//不能写成b = 1了: if(b) { ... } 是可 ...