原题地址

典型的地图寻路问题

如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了。

如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如果当前位置能够提供的体力比向下走所需要的最小体力还多,那么当前位置只需要1的体力就够了;否则,计算出额外需要多少体力。

如果向右走,同理。

设任意坐标(i, j)处最少需要health[i][j]的体力才能通关,则有如下递推公式:

health[i][j] = min{
dungeon[i][j] >= health[i+1][j] ? 1 : health[i+1][j] - dungeon[i][j],
dungeon[i][j] >= health[i][j+1] ? 1 : health[i][j+1] - dungeon[i][j]
}

因为递推公式只用到了相邻层,所以可以在编码时压缩成1维数组节省空间。

代码:

 int calculateMinimumHP(vector<vector<int> > &dungeon) {
if (dungeon.empty() || dungeon[].empty()) return -; int m = dungeon.size();
int n = dungeon[].size();
vector<int> health(n, ); health[n - ] = dungeon[m - ][n - ] >= ? : - dungeon[m - ][n - ]; for (int j = n - ; j >= ; j--)
health[j] = dungeon[m - ][j] >= health[j + ] ? : health[j + ] - dungeon[m - ][j]; for (int i = m - ; i >= ; i--) {
health[n - ] = dungeon[i][n - ] >= health[n - ] ? : health[n - ] - dungeon[i][n - ];
for (int j = n - ; j >= ; j--) {
int right = dungeon[i][j] >= health[j + ] ? : health[j + ] - dungeon[i][j];
int down = dungeon[i][j] >= health[j] ? : health[j] - dungeon[i][j];
health[j] = min(right, down);
}
} return health[];
}

Leetcode#174 Dungeon Game的更多相关文章

  1. [LeetCode] 174. Dungeon Game 地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  2. leetcode@ [174] Dungeon Game (Dynamic Programming)

    https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...

  3. ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  4. Java for LeetCode 174 Dungeon Game

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  5. [leetcode]174. Dungeon Game地牢游戏

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  6. LeetCode 174. Dungeon Game (C++)

    题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...

  7. leetcode 174. 地下城游戏 解题报告

    leetcode 174. 地下城游戏 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格.我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下 ...

  8. 【LeetCode】174. Dungeon Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  9. 【LeetCode】174. Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...

随机推荐

  1. 记 移动端页面中莫名其妙的渲染BUG

    问题描述: 在一个很简单的测试页面中 简单的两块布局,上下两块均没有单独设置字体大小,都用body继承的大小,即40px.我们现在给第一个块.fl  加上浮动 float:left; 另外一个块处于正 ...

  2. devexpress 控制面板汉化方式 参考信息

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 在WPF中调用Winform控件

    最近在项目中用到了人脸识别和指纹识别,需要调用外部设备和接口,这里就用到了在WPF中调用Winform控件. 第一步,添加程序集引用.System.Windows.Forms和WindowsForms ...

  4. 在Linux上安装JDK7

    查看是否安装了JDK 如果安装完毕后,jdk版本不是当前所安装的,则需要卸载之前linux自带的jdk版本,因为安装Redhat9后默认安装了jdk, 可是默认安装的jdk1.4版本比较老,所以需要先 ...

  5. JVM学习总结三——垃圾回收器

    整两天再看调优分析的部分,发现实际运行环境下,还是要考虑配置垃圾回收器,所以这里就加一小章介绍一下. 首先来看一下HotSpot所支持回收期的关系图: 图中可以看到一共有7中垃圾回收器,以中间绿线为界 ...

  6. SQL Server 2008 表变量参数(表值参数)用法

    表值参数是 SQL Server 2008 中的新参数类型.表值参数是使用用户定义的表类型来声明的.使用表值参数,可以不必创建临时表或许多参数,即可向 Transact-SQL 语句或例程(如存储过程 ...

  7. RTP、RTCP

    http://blog.chinaunix.net/uid-22670933-id-1771676.html

  8. Linux下Nginx的安装与配置

    安装前需要安装pcre:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/1.解压缩:     tar xjpf pcre-7.8.tar.b ...

  9. C#类型的转换:Converter<TInput, TOutput> 委托的使用

    Converter<TInput, TOutput> 委托 表示将对象从一种类型转换为另一种类型的方法. 此委托由 Array 类的 ConvertAll<TInput, TOutp ...

  10. 【EF Code First】CodeFirst初始配置

    1,在Nuget管理中下载EntityFramework 2,配置文件中添加数据库配置 <connectionStrings> <add name="DefaultConn ...