原题地址

典型的地图寻路问题

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

如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如果当前位置能够提供的体力比向下走所需要的最小体力还多,那么当前位置只需要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. PHP计算某个目录大小的方法

    用PHP来计算某个目录大小的方法. PHP CURL session COOKIE  可以调用系统命令,还可以这样:  <?php function dirsize($dir) {  @$dh  ...

  2. C++12!配对

    题目内容:找出输入数据中所有两两相乘的积为12!的对数. 输入描述:输入数据中含有一些整数n(1<=n<232). 输出描述:输出所有两两相乘的积为12!的对数. 题目分析:对于输入的每个 ...

  3. Dapper多表查询(列名重复,类字段重复)映射方案

    1. 一个主名,一个别名,设计时候属性和字段命名不同. 这样主名和别名都可以用的,在主名与别人重复时候用别名(别名可以设计的明确一点长一点,比如类名和字段结合) 2. 或者找一个字段多的直接继承出一个 ...

  4. 18.python的异常处理

    python中至少包括两种错误:语法错误(syntax errors)和异常(exceptions). 1.语法错误   语法错误,也被称作解析错误

  5. python分片

    刚刚学习,很新 >>> numbers = [1,2,3,4,5,6,7,8,9,10] >>> numbers[0:10:2] [1,3,5,7,9] >& ...

  6. object sender ,EventArs e

    引用:http://blog.csdn.net/kongbai308416350/article/details/4233786 说的通俗一些,就是: 有一个叫做EventHandler 的家伙,他会 ...

  7. Android--获取使用的总流量和每个App的上传、下载的流量

    获得每个App的上传.下载的流量. 思路就是获取到我们手机上的所有app,再获得app里面使用的权限,如果app有网络权限,就显示出来. 代码很简单,代码里面也有比较详细的注释,下面直接上代码 布局文 ...

  8. microsoft azure 速度测试网址

    http://www.azurespeed.com/ 选择你附近的区域.可以使用 azurespeed.com 查找延迟最低的数据中心.

  9. 关于开始学习Leetcode的第一帖

    从明天开始,白天在实验室完成工作,晚上来图书馆换个环境去学习算法数据结构等计算机基础性的技能.在LeetCode这个平台上做题. 现在感觉自己在算法和数据机构这方面实在是太薄弱了,需要慢慢的捡起来来, ...

  10. MVC4.0 利用HandleErrorAttribute和log4net实现记录异常日志功能

    1.MVC4.0中HandleErrorAttribte已经帮我们处理了异常问题,当我们新建一个非空的MVC项目时候,在FilterConfig中会发现这样的代码 public class Filte ...