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 应用间的集成
第一次在手机上安装wsm tools时发现wsm只是个简单的集成框架,需要用其中的工具还需要单独安装,而安装一个工具以后发现图标没有显示,感觉很神奇,最近工作需要,也要做android应用间的集成,研 ...
- 免费邮件服务器:MailEnable
官方网站地址:www.mailenable.com 下载最新版的 Standard Edition (FREE) 安装之前请留意安装指引就可以了,安装上去之后,直接就可以使用了 安装指引上写的清清楚楚 ...
- Direct3D 11的资源
资源(Resource) 如果把渲染流水线比喻成汽车装配线,资源就是流水线上需要输入的东西. 资源可分为两类:Textures(纹理)和Buffers(缓冲区). Textures可以简单地分为1维, ...
- UESTC 1811 Hero Saving Princess
九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/11104265 题目链接 :http://222.197.181.5/proble ...
- Git实现从本地加入项目到远程仓库
Git是如今最流行的版本号控制系统之中的一个了,今天也试试了.成功了上传了远程仓库,接下来看看我是怎么做的. 1.首先,要有git的账号.点击查看怎么注冊? 2.注冊成功之后.登陆GitHub.然后, ...
- 将samba加入到windows域《转载》
将samba加入到windows域 那什么是域呢? 一台Windows计算机,它要么隶属于工作组,要么隶属于域.所以说到域,我们就不得不提一下工作组,工作组是MS的概念,一般的普遍称谓是对等网. 工作 ...
- F# 越用越喜欢
F# 越用越喜欢 最近由于需要,把遗忘了几年的F#又捡了起来.说捡了起来,倒不如说是从头学习,原来学的早已经忘了!所谓学过,只不过看过一本<F# 语言程序设计> (郑宇军 凌海风 编著 - ...
- sql 2000 分页
create PROCEDURE [dbo].[Proc_GetPageList] ( @Tables varchar(1000), --表名 @PK varchar(100 ...
- 客户Oracle数据库在插入数据的时候报超出最大长度的错误(规避风险)
背景: 项目使用oracle数据,在开发环境测试一些正常.项目部署到客户的服务器上后,系统在添加数据的时候报错.输出错误信息,发现是“超出最大长度”的异常. 但是按照数据库的设计,添加的数据应该在允许 ...
- Paros抓包工具
http://www.hackbase.com/article-1593-1.html http://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1& ...